【发布时间】:2014-09-16 10:17:40
【问题描述】:
我正在使用默认相机意图来捕捉我的活动中的图像。之后,我将它们的路径存储在一个数组中。在活动结束时,我将图像复制到我的应用程序文件夹中。由于某种原因,图像没有以完整质量复制。例如:如果 DCIM 文件夹中的图像为 1.04 MB,那么它在我的应用程序文件夹中只有 ~2KB。
我在我的应用程序中使用此代码。调用相机意图:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
在 onActivity Result 我正在做的事情:
Bitmap photo = (Bitmap) data.getExtras().get("data");
Uri tempUri = getImageUri(context, photo);
imagePath = getRealPathFromURI(tempUri);
imagesList.add(imagePath);
getImageUri() 和 getRealPathFromURI() 方法是:
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(),
inImage, "Title", null);
return Uri.parse(path);
}
public String getRealPathFromURI(Uri uri) {
Cursor cursor = context.getContentResolver().query(uri, null, null,
null, null);
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
return cursor.getString(idx);
}
在我的活动结束时,我正在使用此方法将图像复制到我的应用程序文件夹:
for (int i = 0; i < noteImagesList.size(); i++) {
File fileimg = new File(noteImagesList.get(i));
File newImageFile = new File(parentfolderpath,
i+"_newimage.jpg");
newImageFile.createNewFile();
Bitmap myBitmap = BitmapFactory.decodeFile(fileimg
.getAbsolutePath());
FileOutputStream fOut = new FileOutputStream(
newImageFile);
myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
if (myBitmap != null) {
if (!myBitmap.isRecycled()) {
myBitmap.recycle();
}
myBitmap = null;
}
}
复制后,图像的质量和大小都在下降。 DCIM 文件夹中的图像清晰,大约 1 MB,复制后模糊,大约 1 KB。
谁能告诉我在复制图像时缺少什么?
编辑
如果我将上面的代码用于从图库中选择的图像,但仍然无法使用相机图像,则上面的代码可以正常工作。
编辑 2
我也使用过这段代码,但结果相同。
public void copyFile(File src, File dst) throws IOException {
FileChannel inChannel = new FileInputStream(src).getChannel();
FileChannel outChannel = new FileOutputStream(dst).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} finally {
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
}
}
【问题讨论】:
-
尝试使用
Bitmap.CompressFormat.PNG而不是Bitmap.CompressFormat.JPEG -
@SagarPilkhwal 使用 CompressFormat PNG 时结果相同
-
你为什么要在
getImageUri()中使用inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);和ByteArrayOutputStream bytes = new ByteArrayOutputStream(); -
imagesList.add(imagePath);请举例说明 imagePath 的样子。这不是您在此处拥有的图像的缩略图吗?当然永远不要使用位图工厂来复制文件。 -
我从网上复制了这些方法并应用了。
标签: android image bitmap android-camera-intent android-bitmap