5

I am a new to Android Development. I wish to select an image or a video from the Gallery of an Android Device. Store it in a variable of typeFile. I am doing this, since I need to upload the image/video on dropbox using the Android API from my application. The constructor takes in the fourth parameter of the type File. I am not sure, what to pass as a file since all the examples I searched display the image chosen in an ImageView by using the url and making a bitmap.

imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

Here is the code, I have.

final Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
//to get image and videos, I used a */"
galleryIntent.setType("*/*"); 
startActivityForResult(galleryIntent, 1);

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && resultCode == RESULT_OK) {               
    Uri selectedImageUri = data.getData();
    imagepath = getPath(selectedImageUri);                
    }
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();                
    int columnIndex = cursor.getColumnIndex(projection[0]);
    String filePath = cursor.getString(columnIndex);
    cursor.close();        
    yourSelectedImage = BitmapFactory.decodeFile(filePath);
    return cursor.getString(column_index);
}
AndyN
  • 1,742
  • 1
  • 15
  • 30
pranav shah
  • 115
  • 1
  • 2
  • 7

3 Answers3

7

All you need to do is just create a File variable with the path of image which you've selected from gallery. Change your OnActivityResult as :

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1 && resultCode == RESULT_OK) {               
    Uri selectedImageUri = data.getData();
    imagepath = getPath(selectedImageUri); 
    File imageFile = new File(imagepath);
    }
}
AndyN
  • 1,742
  • 1
  • 15
  • 30
  • oh thanks for notifying me @User22791 . I tried to notify you but I couldn't somehow :) – pranav shah Apr 15 '14 at 07:08
  • @pranavshah you can not tag me on my answer but i get notified when you leave comment on any answer given by me. – AndyN Apr 15 '14 at 07:09
3

this works for image selection. also tested in API 29,30. if anyone needs it.

private static final int PICK_IMAGE = 5;

Intent intent = new Intent(Intent.ACTION_PICK, 
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(Intent.createChooser(intent, "select image"), 
                PICK_IMAGE);

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode ==  PICK_IMAGE && resultCode == RESULT_OK) {               
    Uri selectedImageUri = data.getData();
    String selectedImagePath = getRealPathFromURIForGallery(selectedImageUri);
    File imageFile = new File(selectedImagePath);
    }
} 

public String getRealPathFromURIForGallery(Uri uri) {
    if (uri == null) {
    return null;
    }
    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = this.getContentResolver().query(uri, projection, null, 
    null, null);
    if (cursor != null) {
        int column_index = 
        cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }
    assert false;
    cursor.close();
    return uri.getPath();
}
doli
  • 63
  • 2
  • 5
0

Try this

You can try this also

public void onActivityResult(int requestCode, int resultCode, Intent data)
{

    if (requestCode == 1 && resultCode == RESULT_OK && data != null)
    {
        File destination = new File(Environment.getExternalStorageDirectory(),System.currentTimeMillis() + ".jpg");
 //You will get file path of captured camera image
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        iv_profile.setImageBitmap(photo);
    }
}
Community
  • 1
  • 1
Sunil
  • 3,785
  • 1
  • 32
  • 43
  • This does not bring the original image, but only a thumbnail as stated in the [documentation.](https://developer.android.com/training/camera/photobasics#TaskPhotoView) – alierdogan7 Aug 05 '21 at 07:37