我正在使用ACTION_IMAGE_CAPTURE使用相机捕捉图像。它工作正常,但问题是图像在单击后显示在imageview中,但无法保存在外部或内部存储中。这是我在外部存储中保存图像的代码。
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
File file = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/DashBoard/");
file.mkdirs();
ticket = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DashBoard/Ticket.jpg";
file4 = new File(file, ticket);
try {
FileOutputStream out = new FileOutputStream(file4);
bitmap.compress(Bitmap.CompressFormat.JPEG , 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
ivTicket.setImageBitmap(bitmap);
任何解决方案?
答案 0 :(得分:0)
跟着它 -
private void SaveImage(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ())
file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
并在清单
中添加此内容<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
编辑:通过使用此行,您可以在图库视图中查看已保存的图片。
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
<强> Reference 强>
答案 1 :(得分:0)
使用ImageWorker库轻松保存位图/可绘制对象/ base64。
如何保存
ImageWorker.to(context).
directory("ImageWorker").
subDirectory("SubDirectory").
setFileName("Image").
withExtension(Extension.PNG).
save(sourceBitmap,85)
那样容易。欢迎捐款。
答案 2 :(得分:-1)
试试这个:
private void SaveImage(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
并在清单
中添加此内容<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
另一个例子here