How to get Image URI from Gallery?

user5123500

I need to fetch an image from a gallery. I'm able to open gallery to select a image, but after selecting the image it doesn't return anything. I need to send that fileUri to another activity and display it on ImageView. I'm able to do this camera, like on button click it call the camera and then I capture image and send it to another activity.

But I don't understand what I use for gallery. Someone please help me with this.

Update

This is I'm using to get image from Camera

private void captureImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    // start the image capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}

This is m using to fetch image from gallery But I want to do it in a same way as I'm doing for captureImage(), so that i can send ImageUri to other activity

 private void browseImage(){
    try {
    Intent galleryIntent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        // Start the Intent
         fileUri = getOutputMediaFileUri(PICK_IMAGE);//this is m using for camera 
         Log.w("ImageAddressOnClick pr", ""+fileUri);
        startActivityForResult(galleryIntent, GALLERY_IMAGE_PICK);
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(),
        e.getMessage()+"ye show hora h",
        Toast.LENGTH_LONG).show();
        Log.e(e.getClass().getName(), e.getMessage(), e);
    }

OnActivityResult Method

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // if the result is capturing Image
     super.onActivityResult(requestCode, resultCode, data);



    if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {

            // successfully captured the image
            // launching upload activity
            launchUploadActivity(true);


        } else if (resultCode == RESULT_CANCELED) {

            // user cancelled Image capture
            Toast.makeText(getApplicationContext(),
                    "User cancelled image capture", Toast.LENGTH_SHORT)
                    .show();

        } else {
            // failed to capture image
            Toast.makeText(getApplicationContext(),
                    "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                    .show();
        }


    }
    else if(requestCode == GALLERY_IMAGE_PICK && resultCode == RESULT_OK
            && null != data)
            {
       Uri selectedImage = data.getData();
         String[] filePathColumn = { MediaStore.Images.Media.DATA };
         Log.w("onActivityResult", "chali ye onActivityResult "+selectedImage);
         // Get the cursor
         Cursor cursor = getContentResolver().query(selectedImage,
                 filePathColumn, null, null, null);
         // Move to first row
         cursor.moveToFirst();

         //int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
       //  imgDecodableString = cursor.getString(columnIndex);
         cursor.close();
         launchUploadActivity(true);
            }
          }

private void launchUploadActivity(boolean isImage){
    Intent i = new Intent(MainActivity.this, UploadActivity.class);
    i.putExtra("filePath", fileUri.getPath());
    i.putExtra("isImage", isImage);
    startActivity(i);
}

/**
 * ------------ Helper Methods ---------------------- 
 * */

/**
 * Creating file uri to store image/video
 */
public Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

/**
 * returning image / video
 */
private static File getOutputMediaFile(int type) {

    // External sdcard location
    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            Config.IMAGE_DIRECTORY_NAME);

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(TAG, "Oops! Failed create "
                    + Config.IMAGE_DIRECTORY_NAME + " directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;
}

I'm sending data through launchUploadActivity();

Thanks in Advance :)

Sagar Zala

Change in onActivityResult:

else if(requestCode == GALLERY_IMAGE_PICK && resultCode == RESULT_OK
        && null != data)
        {
            Uri selectedImage = data.getData();         
            String picturePath = getRealPathFromURI(selectedImage,
                    this);
           Intent i = new Intent(MainActivity.this, UploadActivity.class);
           i.putExtra("filePath", selectedImage);
           i.putExtra("isImage", isImage);
           startActivity(i);
        }
      }
}


public String getRealPathFromURI(Uri contentURI, Activity context) {
        String[] projection = { MediaStore.Images.Media.DATA };
        @SuppressWarnings("deprecation")
        Cursor cursor = context.managedQuery(contentURI, projection, null,
                null, null);
        if (cursor == null)
            return null;
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        if (cursor.moveToFirst()) {
            String s = cursor.getString(column_index);
            // cursor.close();
            return s;
        }
        // cursor.close();
        return null;
    }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to get image from gallery

From Dev

how to get image from gallery

From Dev

How to get path for image, selected from gallery or captured through camera

From Dev

How to get an image from gallery after application gets killed?

From Dev

How to get image metadata from ACF gallery images?

From Dev

how to resize image from gallery

From Dev

get selected image from gallery into imageview

From Dev

Android - Get an Image from the Gallery App?

From Dev

Get image from gallery to set in imageview in fragment?

From Dev

Qt Java - Get image from gallery

From Dev

Qt and Android - Get path from image in gallery

From Dev

Get Image from the Gallery and Show in ImageView

From Dev

Android | Can't get image from gallery

From Dev

Android | Can't get image from gallery

From Dev

How to get Woocommerce Product Gallery image URLs?

From Dev

how to get URI of stored images from db and display in image view

From Dev

How to get image height and width from uri on React Native?

From Dev

Android: how to open image from gallery?

From Dev

How to show image from Gallery to ImageView?

From Dev

How to upload image from gallery to server?

From Dev

How to copy an image from phone gallery to project

From Dev

How can i pick a image from gallery?

From Dev

How to upload image from gallery to server?

From Dev

How to hide image from Gallery in Android?

From Dev

How to get image URI in Selenium?

From Dev

How to open the default gallery from android and receive the picked uri of the picture?

From Dev

Android how can i get thumb image of video file from gallery

From Dev

Android - How to do a callback function to get path image selected from gallery

From Dev

how to get images to collectionViewCells from Gallery?

Related Related

  1. 1

    how to get image from gallery

  2. 2

    how to get image from gallery

  3. 3

    How to get path for image, selected from gallery or captured through camera

  4. 4

    How to get an image from gallery after application gets killed?

  5. 5

    How to get image metadata from ACF gallery images?

  6. 6

    how to resize image from gallery

  7. 7

    get selected image from gallery into imageview

  8. 8

    Android - Get an Image from the Gallery App?

  9. 9

    Get image from gallery to set in imageview in fragment?

  10. 10

    Qt Java - Get image from gallery

  11. 11

    Qt and Android - Get path from image in gallery

  12. 12

    Get Image from the Gallery and Show in ImageView

  13. 13

    Android | Can't get image from gallery

  14. 14

    Android | Can't get image from gallery

  15. 15

    How to get Woocommerce Product Gallery image URLs?

  16. 16

    how to get URI of stored images from db and display in image view

  17. 17

    How to get image height and width from uri on React Native?

  18. 18

    Android: how to open image from gallery?

  19. 19

    How to show image from Gallery to ImageView?

  20. 20

    How to upload image from gallery to server?

  21. 21

    How to copy an image from phone gallery to project

  22. 22

    How can i pick a image from gallery?

  23. 23

    How to upload image from gallery to server?

  24. 24

    How to hide image from Gallery in Android?

  25. 25

    How to get image URI in Selenium?

  26. 26

    How to open the default gallery from android and receive the picked uri of the picture?

  27. 27

    Android how can i get thumb image of video file from gallery

  28. 28

    Android - How to do a callback function to get path image selected from gallery

  29. 29

    how to get images to collectionViewCells from Gallery?

HotTag

Archive