Save the image made by user in fingerPaint api demos in android

前端 未结 3 456
情歌与酒
情歌与酒 2020-12-18 17:33

Hey guys I am trying to build a signature task application . In which the user will create the signature on touch and that bitmap should be saved in the phone. I have create

相关标签:
3条回答
  • 2020-12-18 18:06
        final View content = findViewById(R.id.layoutFP);
        content.setDrawingCacheEnabled(true);
        Bitmap bitmap = content.getDrawingCache();
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/saved_images");    
        myDir.mkdirs();
        Random generator = new Random();
        int n = 10000;
        n = generator.nextInt(n);
        String fname = "Image-"+ n +".jpg";
        File file = new File (myDir, fname);
        if (file.exists ()) file.delete (); 
        try {
               FileOutputStream out = new FileOutputStream(file);
               bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
               out.flush();
               out.close();
               content.invalidate(); 
               sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
    
        } catch (Exception e) {
               e.printStackTrace();
        }finally{
            content.setDrawingCacheEnabled(false);                          
        }
    

    Finally add permission in your Manifest file:

    < uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    0 讨论(0)
  • 2020-12-18 18:16

    You've got a Bitmap object in your fingerPaint class. Instantiate a FileOutputStream to the location you'd like to save to, and call mBitmap.compress method with the appropriate options and your output stream. Remember to close your stream.

    0 讨论(0)
  • 2020-12-18 18:21

    Here is the source code to do that

    LinearLayout v = (LinearLayout) findViewById(R.id.mainLayout);
    v.setDrawingCacheEnabled(true);
    // this is the important code :)
    // Without it the view will have a
    // dimension of 0,0 and the bitmap will
    // be null
    v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    v.layout(0, 0, v.getWidth(), v.getHeight());
    v.buildDrawingCache(true);
    Bitmap bm = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false);
    if (bm != null) {
        try {
            String path = Environment.getExternalStorageDirectory().toString();
            OutputStream fOut = null;
            File file = new File(path, "screentest.jpg");
            fOut = new FileOutputStream(file);
            bm.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
            fOut.flush();
            fOut.close();
            Log.e("ImagePath", "Image Path : " + MediaStore.Images.Media.insertImage( getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName()));
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    

    Hope this will work with you also.

    0 讨论(0)
提交回复
热议问题