0
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, 2);
}

protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    Uri selectedImage = intent.getData();
    Bitmap image = BitmapFactory.decodeFile(selectedImage.toString());
    ImageView image1= (ImageView) findViewById(R.id.imageView);
    image1.setImageBitmap(image);
    String ABC ="ABC";
}

I get null value for the Bitmap image. How should I change the code so that I can get the value?

Michaël
  • 3,679
  • 7
  • 39
  • 64

5 Answers5

1

Try this

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.ImageView;


public class MainActivity extends Activity {
    ImageView image1;
    private static final int SELECT_PICTURE = 1;
    private String selectedImagePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image1 = (ImageView) findViewById(R.id.imageView);
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, ""), 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {
            if (data.getData() != null) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                image1.setImageURI(selectedImageUri);
            }

        } else
            super.onActivityResult(requestCode, resultCode, data);
    }

    private String getPath(Uri selectedImageUri) {
        String[] projection = {MediaStore.MediaColumns.DATA};
        @SuppressWarnings("deprecation")
        Cursor cursor = managedQuery(selectedImageUri, projection, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else
            return null;
    }
}
Rajesh
  • 724
  • 8
  • 30
0

No need to convert the Uri to bitmap. Directly call the below method to set the image to image view.

image1.setImageURI(selectedImage);

EDIT

You can get the bitmap from uri like this.

Bitmap image = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
Chandrakanth
  • 3,711
  • 2
  • 18
  • 31
  • i still have to figure out why it can null values, because i need to convert it to base64 to store it in the database later on. –  Jan 05 '15 at 11:01
  • I think you missed read external storage permission. Just add the and check once. – Chandrakanth Jan 05 '15 at 11:50
  • 01-05 22:31:31.456 16473-16473/com.example.lean.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.lean.myapplication, PID: 16473 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { dat=content://media/external/images/media/242424 }} to activity {com.example.lean.myapplication/com.example.lean.myapplication.MyActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri –  Jan 05 '15 at 14:32
  • Add in manifest file...it will works. – Chandrakanth Jan 05 '15 at 16:15
0

Use this code for getting image from gallery.

 Uri selectedImage = data.getData();
 String[] filePath = { MediaStore.Images.Media.DATA };
 Cursor c = getContentResolver().query(selectedImage, filePath,
                    null, null, null);
 c.moveToFirst();
 int columnIndex = c.getColumnIndex(filePath[0]);
 picturePath = c.getString(columnIndex);
 c.close();
 setPic(picturePath);


private void setPic(final String path) {
    int targetW = imageview.getWidth();
    int targetH = imageview.getHeight();

    final BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    int scaleFactor = Math.min(photoW / targetW, photoH / targetH);

    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    bitmap = BitmapFactory.decodeFile(path, bmOptions);
    imageview.setImageBitmap(bitmap);
  }
RockStar
  • 409
  • 2
  • 6
0

Try this...

   protected void onActivityResult(int requestCode, int resultCode, Intent intent){
  if (requestCode == 2) {
        Uri selectedImage = intent.getData();
        String fileUriString = selectedImage .toString().replaceFirst("file:///", "/").trim();
        Bitmap image = BitmapFactory.decodeFile(selectedImage.toString());
        ImageView image1= (ImageView) findViewById(R.id.imageView);
        image1.setImageBitmap(image);
        String ABC ="ABC";
}
    }
chain
  • 649
  • 10
  • 22
  • what are you getting ? print fileUriString value whether it contains value or not. – chain Jan 05 '15 at 11:22
  • I just edit my code.. try that if it doesn't work refer this blog http://www.coderzheaven.com/2012/04/20/select-an-image-from-gallery-in-android-and-show-it-in-an-imageview/ – chain Jan 05 '15 at 11:29
0

Try the below code this wokrs well for me :

Open the gallery and select your image :

Intent g = new Intent(Intent.ACTION_GET_CONTENT);
g.setType("image/*");
startActivityForResult(g, FROM_GALLERY);

FROM_GALLERY is defined as int.

Get the image and set it to imageView :

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

     if (requestCode == FROM_GALLERY 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();

         ImageView image1 = (ImageView) findViewById(R.id.imageView);
         image1.setImageBitmap(BitmapFactory.decodeFile(picturePath));
  }
}

Hope this helps you!!!

Josef
  • 442
  • 7
  • 16
  • Have a look at this for some explication : http://stackoverflow.com/questions/2507898/how-to-pick-an-image-from-gallery-sd-card-for-my-app?lq=1 – Josef Jan 05 '15 at 12:30
  • 01-05 22:42:21.916 22150-22150/com.example.lean.myapplication E/BitmapFactory﹕ Unable to decode stream: java.lang.NullPointerException –  Jan 05 '15 at 14:43