保存在内部存储中的位图不显示在图库中
Bitmap saved in internal storage doesn't show on Gallery
我创建了一个简单的应用程序来将位图保存在内部存储器中,并能够像显示任何其他图像一样在 android 图库中显示此位图。
问题是:保存后没有在图库中显示...而且我不知道我做错了什么。 (我正在模拟器上对此进行测试)
这是我的 class 处理位图 的保存。当我点击一个按钮保存位图时,这个 Saver class 在我的主要 activity 中使用。
public class Saver {
private Context mContext;
private String mNameOfFolder = "Storager";
private String mNameOfFile = "Quote";
public void saveImage(Context context, Bitmap imageToSave) {
mContext = context;
// Create the directory
File dir = mContext.getDir(mNameOfFolder, Context.MODE_PRIVATE);
String filePath = dir.getAbsolutePath();
// Get the current date and time to attach to the name of the image
String currentDateAndTime = getCurrentDateAndTime();
if(!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, mNameOfFile + currentDateAndTime + ".jpg");
try {
FileOutputStream out = new FileOutputStream(file);
imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
galleryAddPic(file);
successSave();
} catch (FileNotFoundException e) {
Log.d("d", "file not found");
unableToSave();
} catch (IOException e) {
Log.d("d", "IO Exception");
unableToSave();
}
}
private void galleryAddPic(File file) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
mContext.sendBroadcast(mediaScanIntent);
}
private String getCurrentDateAndTime() {
Calendar calendar = Calendar.getInstance();
// Setting format of the time
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
// Getting the formated date as a string
String formattedDate = dateFormat.format(calendar.getTime());
return formattedDate;
}
}
我什至添加了 android 开发者网站 here 提供的 galleryAddPic() 方法,以便能够将图像添加到媒体提供商的数据库中,使其在 Android 图库应用程序和其他应用程序。
包括 MediaStore
在内的第三方应用无法访问您应用的 internal storage. If you want third-party apps to have access to the file, use external storage。
另外,我不知道你为什么要在这里创建 ContextWrapper
。如果您有需要 Context
的方法(例如,您当前代码中的 getDir()
),请在传递给您的方法的 Context
上调用它们。
我创建了一个简单的应用程序来将位图保存在内部存储器中,并能够像显示任何其他图像一样在 android 图库中显示此位图。
问题是:保存后没有在图库中显示...而且我不知道我做错了什么。 (我正在模拟器上对此进行测试)
这是我的 class 处理位图 的保存。当我点击一个按钮保存位图时,这个 Saver class 在我的主要 activity 中使用。
public class Saver {
private Context mContext;
private String mNameOfFolder = "Storager";
private String mNameOfFile = "Quote";
public void saveImage(Context context, Bitmap imageToSave) {
mContext = context;
// Create the directory
File dir = mContext.getDir(mNameOfFolder, Context.MODE_PRIVATE);
String filePath = dir.getAbsolutePath();
// Get the current date and time to attach to the name of the image
String currentDateAndTime = getCurrentDateAndTime();
if(!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, mNameOfFile + currentDateAndTime + ".jpg");
try {
FileOutputStream out = new FileOutputStream(file);
imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
galleryAddPic(file);
successSave();
} catch (FileNotFoundException e) {
Log.d("d", "file not found");
unableToSave();
} catch (IOException e) {
Log.d("d", "IO Exception");
unableToSave();
}
}
private void galleryAddPic(File file) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(file);
mediaScanIntent.setData(contentUri);
mContext.sendBroadcast(mediaScanIntent);
}
private String getCurrentDateAndTime() {
Calendar calendar = Calendar.getInstance();
// Setting format of the time
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
// Getting the formated date as a string
String formattedDate = dateFormat.format(calendar.getTime());
return formattedDate;
}
}
我什至添加了 android 开发者网站 here 提供的 galleryAddPic() 方法,以便能够将图像添加到媒体提供商的数据库中,使其在 Android 图库应用程序和其他应用程序。
包括 MediaStore
在内的第三方应用无法访问您应用的 internal storage. If you want third-party apps to have access to the file, use external storage。
另外,我不知道你为什么要在这里创建 ContextWrapper
。如果您有需要 Context
的方法(例如,您当前代码中的 getDir()
),请在传递给您的方法的 Context
上调用它们。