Android将位图保存到SD卡

我有一个button,我想当我点击它时,图像被保存到SD卡(或内部存储,如htc一个x我们没有一个像SD卡一样的外部存储)

这是我的代码:

sd.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub MpClick.start(); File myDir=new File("/sdcard/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); bitMapToShare.compress(Bitmap.CompressFormat.JPEG, 600, out); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } } }); 

以及如何在消息中显示“您的图像已保存”。 像一个警报,但2秒,然后消失

Solutions Collecting From Web of "Android将位图保存到SD卡"

尝试这个

 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(); } } 

并将其添加到清单中

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

看看这个答案Android保存文件到外部存储

编辑 :通过使用这一行,你可以看到图库视图中保存的图像。

 sendBroadcast(new Intent( Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); 

使用Toast消息

喜欢

 Toast.makeText(Your_class_name.this, "Your image is saved to this folder", Toast.LENGTH_LONG) .show();