将图像从DCIM文件夹复制到另一个文件夹会导致图像质量损失



我在活动中使用默认的相机意图来捕捉图像。之后,我将它们的路径存储在一个数组中。活动结束时,我将图像复制到我的应用程序文件夹中。由于某些原因,图像没有完全复制。例如:如果DCIM文件夹中的图像是1.04MB,那么它在我的应用程序文件夹中只有~2KB。

我正在我的应用程序中使用此代码。用于调用相机意图:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);

在我正在做的onActivity结果中:

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文件夹中的图像清晰且约为1MB,则复制后图像模糊且约为1KB。

有人能说出我在复制图像时遗漏了什么吗?

编辑

上面的代码工作得很好,如果我使用它的图像从画廊选择,但仍然没有运气的相机图像。

编辑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();
    }
}

文档中说,如果您传递一个指示在哪里写入文件的URI,那么您将在额外数据中返回完整大小的图像。但是,如果不传递URI,则会返回一个小版本(缩略图)。

由于您没有传递URI,因此您正在获取缩略图,然后保存。/复制,而不是全尺寸版本。

最新更新