ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-11-12 00:25:53  阅读:163  来源: 互联网

标签:android-bitmap android-gallery bitmap android


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

到目前为止,我做到了:

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拍摄新图片然后解码为位图时,它可以完美工作,但是当我从图库中选择图片并解码为位图时,它不起作用.

解决方法:

使用此代码

    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-bitmap,android-gallery,bitmap,android
来源: https://codeday.me/bug/20191111/2023440.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有