Search code examples
androidbitmapandroid-galleryandroid-bitmap

Android Copy Image From Gallery Into New File


I'm trying to implement a feature for my app which allows users to pick a picture from the gallery for some proposals. I need to save this picture as a new picture before apply the changes (filters, crop, etc).

So far I did:

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){

        }
    }
}

The code to get a new File (FunctionUtil.getOutputMediaFile) , the code to refresh gallery (FunctionUtil.refreshMediaGallery) and the code to save the bitmap (Bitmap.compress) works fine in different parts of the same activity but with pictures from gallery It just not save them!

It works perfectly when I take a new picture using camera API then decode to a bitmap but it's not working when I pick the picture from gallery and decode to a bitmap.


Solution

  • Use this code

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

    and use the following method

    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);
            }
        }
    

    or you can also use com.google.api.client.util.IOUtils.copy(InputStream, OutputStream)