Android将位图保存到SD卡
我有一个按钮, 我想当我点击它的时候,图像会被保存到sd卡中(或者内部存储器,就像htc one x一样,我们没有像sd卡那样的外部存储器) 这是我的代码:Android将位图保存到SD卡,android,image,bitmap,alert,Android,Image,Bitmap,Alert,我有一个按钮, 我想当我点击它的时候,图像会被保存到sd卡中(或者内部存储器,就像htc one x一样,我们没有像sd卡那样的外部存储器) 这是我的代码: sd.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub
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秒后消失使用Toast消息 像 试试这个
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" />
Toast.makeText(this,R.string.your_message,Toast.LENGTH_SHORT.show)怎么样;它的可能副本工作正常,只有一个问题,相册“saved_images”没有出现在gallery中,但使用文件管理器,我可以看到它制作了一个文件夹“saved_images”,其中有我保存的图像。。我如何制作它,将它保存在gallery@AhmedAl-ekrii您可能正在emulator上测试此功能,并且emulator更新缓慢。在进入多媒体资料之前使用媒体触发器。您将在那里找到指定的文件夹。不,我正在使用我的手机HTC One X。。使用媒体触发器是什么意思?(对不起,我是初学者)没关系,问题是对于任何面临我问题的人来说,库缓存都是问题,使用此代码,它可以工作sendBroadcast(新的Intent(Intent.ACTION\u MEDIA\u MOUNTED,Uri.parse(“file://“+Environment.getExternalStorageDirectory());为了避免重复,最好使用日期戳而不是随机数。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));