从Android相册复制图像到新文件

3
我将尝试为我的应用程序实现一个功能,允许用户从图库中选择图片以进行某些操作。在应用更改(滤镜、裁剪等)之前,我需要将此图片保存为新图片。
到目前为止,我的做法是:
private void pickImageFromGallery(){
    /*Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("image/*");
    startActivityForResult(intent, GALLERY_SELECT_PICTURE);*/

    Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT);
    getIntent.setType("image/*");

    Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    pickIntent.setType("image/*");

    Intent chooserIntent = Intent.createChooser(getIntent, "");
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent});

    startActivityForResult(chooserIntent, GALLERY_SELECT_PICTURE);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == RESULT_OK){
        if(requestCode == GALLERY_SELECT_PICTURE){
            if(data == null){
                //TODO SHOW ERROR
                return;
            }
            try {
                Bitmap temporaryBitmap = MediaStore.Images.Media.getBitmap(myContext.getContentResolver(), data.getData());

                //Tried using inputStream and got the same result
                //InputStream inputStream = myContext.getContentResolver().openInputStream(data.getData());
                //Bitmap temporaryBitmap = BitmapFactory.decodeStream(inputStream, null, options);

                //Just return a file to save the bitmap into (I use the same code in different activities and it works perfectly)
                capturedImage = FunctionUtil.getOutputMediaFile(ConstUtil.ids.MEDIA_TYPE_IMAGE);

                FileOutputStream outputStream = new FileOutputStream(capturedImage);;
                temporaryBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);

                //Just refresh the gallery so my new picture becomes available (I use the same function in different activities and it works fine)
                FunctionUtil.refreshMediaGallery(capturedImage);

             //HERE I CHECK THE PATH/GALLERY AND NOTICE THE FILE ISN'T SAVED 

            } catch (Exception e) {
                e.printStackTrace();
                //TODO SHOW ERROR
            }

        }else if(requestCode == GALLERY_SELECT_VIDEO){

        }
    }
}

FunctionUtil.getOutputMediaFile 函数用于创建新文件,FunctionUtil.refreshMediaGallery 函数用于刷新图库,Bitmap.compress 函数用于保存位图。这些函数在同一活动的不同部分可以正常使用,但是对于来自图库的图片,它们无法保存。

当我使用相机API拍摄新照片并解码为位图时,它能够完美运行,但是当我从图库中选择图片并解码为位图时则不能正常工作。

1个回答

4

请使用此代码

    if(requestCode == GALLERY_SELECT_PICTURE){
       InputStream inputStream = getContentResolver()
            .openInputStream(data.getData());
       FileOutputStream fileOutputStream = new FileOutputStream(
                                    outputFile);
       copyStream(inputStream, fileOutputStream);
       fileOutputStream.close();
       inputStream.close();
}

并使用以下方法。
public static void copyStream(InputStream input, OutputStream output)
            throws IOException {

        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = input.read(buffer)) != -1) {
            output.write(buffer, 0, bytesRead);
        }
    }

或者,您也可以使用com.google.api.client.util.IOUtils.copy(InputStream, OutputStream)


1
这绝对是最好的答案和方法。解析并尝试操作结果Uri会让你发疯(而且甚至不确定是否正确)。IOUtils现在已经过时,您可以使用inputStream.copyTo(outputStream) - Miguel Lasa

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接