Android 将图库中的图像复制到新文件中

标签 android bitmap android-gallery android-bitmap

我正在尝试为我的应用程序实现一项功能,该功能允许用户从画廊中为某些提案选择图片。在应用更改(滤镜、裁剪等)之前,我需要将此图片另存为新图片。

到目前为止我做了:

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) 在同一 Activity 的不同部分工作正常,但使用图库中的图片只是不保存它们!

当我使用相机 API 拍摄一张新照片然后解码为位图时,它工作得很好,但当我从图库中挑选照片并解码为位图时它不起作用。

最佳答案

使用此代码

    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)

关于Android 将图库中的图像复制到新文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41182198/

相关文章:

java - 光标错误 - 从图库中检索图像

android - 图库 View 不是从左边开始

java - 当离开主线程时,我怎样才能让一些代码尽快在主线程上运行?

java - 全局使用 PersistentCookieStore

delphi - 如何更改 32 位 TBitmap 中特定颜色的 alpha 值?

android - 在位图上绘制文本失败

java - `getExternalStorageDirectory()` 问题 - Android

android - 如何在Android中的imageview上设置从SD卡中选择的图像

java - setMovementMethod(滚动)干扰 textView 中的 scaleGestureDetector(缩放)

c# - 从 MVVM/WPF 中的 BackgroundWorker 更新位图