【问题标题】:Copying images from the DCIM folder to another folder cause loss of image quality将图像从 DCIM 文件夹复制到另一个文件夹会导致图像质量下降
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-30
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 2018-03-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多