Photo Albums and dropbox application

1.2k views Asked by At

This is how i am firing an intent to get the picture from photo albums

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, SELECT_PICTURE);

then from the result i take the path from the

 Uri selectedImageUri = data.getData();

public String getPath(Uri selectedImageUri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

of selected picture and put that image in sdcard.

But as the dropbox application is also installed then that also comes up and when i select image then Uri comes like

file:///mnt/sdcard/Android/data/com.dropbox.android/files/scratch/FloorPlanImage/7th_floor_new.jpg

and now when i try getPath it crashes . Can you tell me how to fix this so that i can take the path of image and save it in the sdcard?

2

There are 2 answers

0
Amit Hooda On BEST ANSWER

Well i got what the problem was

int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

returns nothing as the image was selected from Dropbox and not from photo gallery of android.

So i did was use the Uri when image is selected and used that to set the ImageView and it worked great.

Thanks !!

0
user2935301 On

Use following code this is working perfect for me

Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(
                    Intent.createChooser(intent, "Upload photo"),
                    RESULT_LOAD_IMAGE);

In 'onActivityResult' write following code

protected void onActivityResult(final int requestCode, int resultCode,
            Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
                && null != data) {


Uri selectedImage = data.getData();
imageView.setImageURI(selectedImage);


        } else

        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
            photo = (Bitmap) data.getExtras().get("data");
            imageView.setImageBitmap(photo);

        }
}