本文介绍了Android 将位图保存到 SD 卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个按钮,我希望当我点击它时,图像被保存到 SD 卡(或内部存储,如在 htc one x 中我们没有像 SD 卡这样的外部存储)
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 sd card )
这是我的代码:
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 秒然后消失
and how do I make a message appears in it it's written "Your image was saved."like an alert but for 2seconds and then disappears
推荐答案
试试这个
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 将文件保存到外部存储
EDIT :通过使用此行,您可以在图库视图中查看保存的图像.
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())));
这篇关于Android 将位图保存到 SD 卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!