[英]Android saving Bitmap to SD card
我有一個按鈕,我希望當我點擊它時圖像被保存到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秒然后消失
試試這個
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.