Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio get File from Gallery Intent

Short answer is it possible to get original file from Gallery request,and if it possible how can i do it? This code doesn't work for me.

Uri uri = data.getData();
File file = new File(uri.getPath());

And the long Answer)): I use this code to make gallery intent

addGallery.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
             Intent intent = new Intent(Intent.ACTION_PICK);
             intent.setType("image/*");
             startActivityForResult(intent, GALLERY_IMAGE_REQUEST);
        }
    });

In mu onActivityResult i use this code

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK)
    {
        switch (requestCode)
        {
            case GALLERY_IMAGE_REQUEST:
                if (data != null)
                {
                    try
                    {
                        Uri uri = data.getData();
                        File file = new File(uri.getPath());
                        FileInputStream inputStream = new FileInputStream(file);
                        bitmap = BitmapFactory.decodeStream(inputStream);
                        imageView.setImageBitmap(bitmap);
                        inputStream.close();
                    } catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
        }
    }
}

And i cant get file.

The same code with getting bitmap from data works well but i need to get exactly file from gallery but not only Uri or Bitmap.

                 try
                    {
                        Uri uri = data.getData();
                        InputStream imageStream = getContentResolver().openInputStream(uri);
                        final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                        imageView.setImageBitmap(selectedImage);
                        imageStream.close();
                    } catch (Exception e)
                    {
                        e.printStackTrace();
                    }
like image 718
Bios90 Avatar asked Nov 20 '25 15:11

Bios90


1 Answers

If you want to import a picture from gallery into your app (in a case your app own it), you need to copy it to your app data folder.

in your onActivityResult():

    if (requestCode == REQUEST_TAKE_PHOTO_FROM_GALLERY && resultCode == RESULT_OK) {
        try {
            // Creating file
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                Log.d(TAG, "Error occurred while creating the file");
            }

            InputStream inputStream = getActivity().getContentResolver().openInputStream(data.getData());
            FileOutputStream fileOutputStream = new FileOutputStream(photoFile);
            // Copying
            copyStream(inputStream, fileOutputStream);
            fileOutputStream.close();
            inputStream.close();

        } catch (Exception e) {
            Log.d(TAG, "onActivityResult: " + e.toString());
        }
    }

Creating the file method:

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

Copy 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);
    }
}
like image 51
MohammadL Avatar answered Nov 22 '25 06:11

MohammadL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!