在Android的位图保存为JPEG外置存储的文件夹中 [英] Save Bitmap in Android as JPEG in External Storage in a folder
本文介绍了在Android的位图保存为JPEG外置存储的文件夹中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用这code保存位图的外部存储,但它不会创建该文件夹,如果它不存在:
I am using this code to save Bitmap in External Storage but it does not create the folder if it not exists:
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOutputStream = null;
File file = new File(path + "/Captures/", "screen.jpg");
try {
fOutputStream = new FileOutputStream(file);
capturedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOutputStream);
fOutputStream.flush();
fOutputStream.close();
MediaStore.Images.Media.insertImage(getContentResolver(), file.getAbsolutePath(), file.getName(), file.getName());
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
return;
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Save Failed", Toast.LENGTH_SHORT).show();
return;
}
我如何保存图片在新的目录,如果不存在,并保存默认如果文件夹是那里的设备?
How can I save the image in the new directory if not exists and save default if the folder is there in the device?
推荐答案
试试这个它会插上U造成肯定的:
try this it will gives u result sure:
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/req_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
file = new File(myDir, fname);
Log.i(TAG, "" + file);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
添加这一项的画廊显示:
add this one to show in gallery:
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
看一下这个链接,明确的答案: 在画廊 显示文件夹的图像
look at this link for clear answer: show folder images in gallery
这篇关于在Android的位图保存为JPEG外置存储的文件夹中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文