我使用下面的代码将位图图像(从布局中捕获)保存到android默认图片目录。似乎保存的图像已损坏,因为Gallery无法打开它。
当我将位图保存在另一个位置时,图库可以打开它。但是当我将它保存到android默认目录时它没有打开。
public void saveToGallery() {
String path = Environment.getExternalStorageDirectory().toString()
+ "/Pictures/Keshavarzi/" + "screenshot-" + System.currentTimeMillis() + ".png";
ViewGroup v = (ViewGroup) findViewById(R.id.lyt_main_report_activity);
v.setDrawingCacheEnabled(true);
v.setDrawingCacheEnabled(true);
v.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false);
OutputStream out = null;
File imageFile = new File(path);
try {
out = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (Exception exc) {
}
}
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "Title");
values.put(MediaStore.Images.Media.DESCRIPTION, "Description");
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
values.put(MediaStore.MediaColumns.DATA, path);
getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
MHToast.showToast(getString(R.string.saved_in_gallery), Toast.LENGTH_LONG);
}
答案 0 :(得分:0)
试试这个,检查你的保存目录是否存在或先创建目录然后保存位图,
String path = Environment.getExternalStorageDirectory().toString()
+ "/Pictures/Keshavarzi/" + "screenshot-" +
System.currentTimeMillis() + ".png";
File imageFile = new File(path);
if(!imageFile.getParentFile().exists()){
imageFile.getParentFile().mkDirs();
}