0

I am new to Android.I have a problem with select an image and return result. I have a method is getImageFromGallery() my scope is this method call then i will choose a image from gallery and return as bitmap.But the problem is onActivityResult() set the bitmap after that bitmap is return. If i pick a image then, should return that selected image bitmap file.please help to find out the solution.

Here is my code.

private Bitmap bitmap;

 public Bitmap getImageFromGallery(){
    Intent i = new Intent(
            Intent.ACTION_PICK,
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, RESULT_LOAD_IMAGE);
    return  bitmap;}


@Override
protected void onActivityResult(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();


        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        bitmap = BitmapFactory.decodeFile(picturePath);
    }

}

I want to return selected bitmap value.

Gamsh
  • 545
  • 4
  • 21
  • "If i pick a image then, should return that selected image bitmap file" -- that is not possible. `startActivityForResult()` is not a blocking call. You will need to adjust your application logic to be able to use the image if and when you get one in `onActivityResult()`. – CommonsWare Dec 04 '17 at 17:26
  • @CommonsWare Then how can handle the problem? can we hold the return until image set to `bitmap` – Gamsh Dec 04 '17 at 17:45
  • "Then how can handle the problem?" -- as I wrote, you will need to adjust your application logic to be able to use the image if and when you get one in `onActivityResult()`. You cannot write a method that returns the bitmap. "can we hold the return until image set to bitmap" -- no, sorry. – CommonsWare Dec 04 '17 at 17:46

1 Answers1

0

Try this:

public void getImageFromGallery(){
    Intent i = new Intent(Intent.ACTION_PICK);
    i.setType("image/*");
    startActivityForResult(i, RESULT_LOAD_IMAGE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK){
        Uri imageUri = intent.getData();
        mBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
    }
}

Your getImageFromGallery can't return the Bitmap as you haven't selected it yet. You can only set it in the onActivityResult.

See more here.

Eselfar
  • 3,759
  • 3
  • 23
  • 43
  • In my case select image is not a problem I want to wait to return bitmap that after set the selected one. – Gamsh Dec 04 '17 at 17:42
  • This is exactly what this code does. You get the image only when the user has really selected it. – Eselfar Dec 04 '17 at 17:45
  • yes in my case user also select, but my problem that method return bitmap before select image. – Gamsh Dec 04 '17 at 17:47