Android saving Bitmap to SD card

后端 未结 2 482
你的背包
你的背包 2020-11-29 05:15

I have a button, and I want when I click on it the image gets saved into the sd card ( or the internal storage, as in htc one x we don\'t have an external storage like an s

相关标签:
2条回答
  • 2020-11-29 05:58

    try this

    private void SaveImage(Bitmap finalBitmap) {
    
       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);
           finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();
    
       } catch (Exception e) {
           e.printStackTrace();
       }
    }
    

    and add this in manifest

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

    Look at this answer Android saving file to external storage

    EDIT : By using this line you can able to see saved images in the gallery view.

    sendBroadcast(new Intent(
    Intent.ACTION_MEDIA_MOUNTED,
            Uri.parse("file://" + Environment.getExternalStorageDirectory())));
    
    0 讨论(0)
  • 2020-11-29 05:58

    Use Toast message

    like

    Toast.makeText(Your_class_name.this,
                        "Your image is saved to this folder", Toast.LENGTH_LONG)
                        .show();
    
    0 讨论(0)
提交回复
热议问题