【发布时间】:2015-11-09 23:19:31
【问题描述】:
我创建了一个简单的应用程序来将位图保存在内部存储中,并且能够像任何其他图像一样在 android 库中显示该位图。
问题是:保存后它没有显示在画廊中......而且我不知道我做错了什么。 (我在模拟器上测试)
这是我处理位图保存的类。当我单击按钮保存位图时,此 Saver 类在我的主要活动中使用。
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(),以便能够将图像添加到 Media Provider 的数据库中,使其在 Android Gallery 应用程序和其他应用程序中可用。
【问题讨论】:
标签: java android bitmap android-gallery