Java 保存在内部存储器中的位图不';t在画廊展出
我创建了一个简单的应用程序,将位图保存在内部存储器中,并能够像其他任何图像一样在android gallery中显示此位图 问题是:保存后它不会显示在库中。。。我不知道我做错了什么。(我正在emulator上测试) 这是我处理位图保存的类。当我点击按钮保存位图时,这个保护程序类在我的主要活动中使用Java 保存在内部存储器中的位图不';t在画廊展出,java,android,bitmap,android-gallery,Java,Android,Bitmap,Android Gallery,我创建了一个简单的应用程序,将位图保存在内部存储器中,并能够像其他任何图像一样在android gallery中显示此位图 问题是:保存后它不会显示在库中。。。我不知道我做错了什么。(我正在emulator上测试) 这是我处理位图保存的类。当我点击按钮保存位图时,这个保护程序类在我的主要活动中使用 public class Saver { private Context mContext; private String mNameOfFolder = "Storager"; private S
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开发者网站提供的galleryAddPic()方法,以便能够将图像添加到媒体提供商的数据库中,使其在android Gallery应用程序和其他应用程序中可用。第三方应用程序,包括
MediaStore
,无法访问您的应用程序。如果希望第三方应用程序能够访问该文件,请使用
另外,我不知道为什么要在这里创建
ContextWrapper
。如果您有需要上下文的方法(例如,当前代码中的getDir()
),请在传递到方法中的上下文中调用它们。编辑了ContextWrapper问题。。。(我一定是在编码时睡着了…)所以我读到的外部存储不仅仅是sd卡,对吧?@Cap.Alvez:很少。在绝大多数安卓设备上,外部存储和内部存储一样都是车载闪存的一部分。区别在于外部存储是公共的(具有适当的权限),而内部存储是私有的。我现在有点困惑。我应该使用什么作为保存外部存储的路径?我想创建一个包含位图的应用程序名称的文件夹,但仍然可以在默认的android gallery中看到它们。例如,我应该使用环境.getExternalStorageDirectory()
?因为如果我使用它,它会指向“/storage/sdcard”,并给我fileNotFoundException.@Cap.Alvez:“我应该使用什么作为在外部存储上保存的路径?”--这在中有介绍。“因为如果我使用它指向“/storage/sdcard”,它会给我fileNotFoundException”--我不知道“it”是什么。如果在模拟器上进行测试,请确保AVD配置具有外部存储。解决了问题:我的AVD没有正确配置外部存储。谢谢,顺便说一句,很好的博客。