【发布时间】:2017-02-27 13:49:42
【问题描述】:
我在android studio 工作。构建一个我正在使用相机的app。当我运行我的应用程序时,该应用程序运行良好。我捕获了它确实捕获了图像的图像。但是我创建的文件夹没有显示在我的画廊中。我将图像保存在我的 local storage 中,而不是 SD CARD 中。我很好奇为什么没有创建该文件夹,因为它没有给我任何错误,所以它应该在我的画廊中。所以我重新启动了我的设备,重新启动后我可以看到我画廊中的文件夹和其中拍摄的图像。我再次打开应用程序并从中获取图像,但图像再次未显示在文件夹中。
下面是我的代码,我在其中制作 ta 目录以保存图像
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE)
{
if(resultCode == Activity.RESULT_OK)
{
Bitmap bmp = (Bitmap)data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
// convert byte array to Bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
if(isStoragePermissionGranted())
{
SaveImage(bitmap);
}
}
}
private void SaveImage(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().getAbsolutePath().toString();
Log.v(LOG_TAG, root);
File myDir = new File(root + "/captured_images");
myDir.mkdirs();
Random generator = new Random();
int n = 1000;
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,100,out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
下面是我调试的图片
**注意:**
由于我使用的是本机相机,因此图片保存在相机胶卷文件夹中,即我设备中的默认文件夹。但是那里保存的图像不是压缩的,压缩图像应该保存在我创建的文件夹中。
我坚持下去,不知道该怎么办。
任何帮助将不胜感激。
【问题讨论】:
-
在
saveImage方法调用这个sendBroadcast(new Intent( Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()))); -
尝试并收到此错误
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=Intent { act=inline-data (has extras) }} to activity -
在 Android 4.4 之前
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));从 Android 4.4sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
标签: android image android-camera save-image