[英]STORING IMAGE using MediaStore.Images.media to save in a particular folder
I have to save a bitmap in a particular folder.我必须将 bitmap 保存在特定文件夹中。 If the folder is not created, I want to create it.
如果没有创建文件夹,我想创建它。
I know you can just save an image like that:我知道你可以保存这样的图像:
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap,
bitmap + ".jpg Card Image", bitmap + ".jpg Card Image");
But how can I save it in a specific location like "MediaStore/folder"(Don't actually know how the path should be) where folder is a folder that I want to create (if it's not created).但是如何将它保存在特定位置,例如“MediaStore/folder”(实际上不知道路径应该如何),其中文件夹是我要创建的文件夹(如果未创建)。
I've tried a lot of things and they didn't work so I would be very thankful if someone could help me!我已经尝试了很多东西,但它们没有用,所以如果有人可以帮助我,我将非常感激!
You can use internal storage like so if you're willing to not use MediaStore如果您愿意不使用 MediaStore,您可以像这样使用内部存储
void saveBitmap(Bitmap bitmap) {
File dir = getFilesDir(); // or getCacheDir()
if (!new File(dir + "/folderName").isDirectory()) {
if (new File(dir + "/folderName").mkdir()) { // directory made successfully
dir = new File(dir + "/folderName/" + "fileName.ext");
} else { // error making the directory
return;
}
} else {
dir = new File(dir + "/folderName/" + "fileName.ext");
}
try {
FileOutputStream fileOutputStream = new FileOutputStream(dir);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream);
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
To retrieve the bitmap检索 bitmap
Bitmap loadBitmap(File dir) {
Bitmap bitmap = null;
try {
FileInputStream fileInputStream = new FileInputStream(dir);
bitmap = BitmapFactory.decodeStream(fileInputStream);
fileInputStream.close();
} catch(Exception e) {
e.printStackTrace();
}
return bitmap;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.