Balkyto
Balkyto

Reputation: 1510

How to load image from gallery to intent

I'm trying to load image (not URL) from either camera or gallery and save it to global class. (At the moment I'm trying to get to the image, no class defined yet).

So I think camera returns image correctly, and put it in the bundle, and I like to use same approach for Gallery if this is possible.

So I have:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode==RESULT_OK){
        Bundle extras = data.getExtras();
        bmp = (Bitmap) extras.get("data");

    }
} 

And this two selections, where obviously I do something wrong with gallery:

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
    switch(arg2){
    case 0:
        i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(i, cameraData);
        break;
    case 1:

        Intent intent = new Intent( Intent.ACTION_GET_CONTENT );
        intent.setType( "image/*" );

        //i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, 10); 
        break;
    }

I'm getting failure delivering result: null pointer exception on resource: dat=content://media/external/images/media/23

So I guess I do something wrong.

Idea is similar to behavior seen in Instagram, take photo or select existing one, and when selected it should be stored in some singletone object, as I'll have 3 more options that can be selected before image is shown again within my app.

I'm not sure if this is the optimal way to handle image, so any suggestion here is also welcome.

Tnx

Upvotes: 0

Views: 539

Answers (1)

morodomi
morodomi

Reputation: 471

onActivityResult, try this code.

InputStream in = getContentResolver().openInputStream(data.getData());
// get picture size.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, options);
in.close();
// resize the picture for memory.
int width = options.outWidth / displayWidth + 1;
int height = options.outHeight / displayHeight + 1;
int sampleSize = Math.max(width, height);
options.inSampleSize = sampleSize;
options.inJustDecodeBounds = false;
in = getContentResolver().openInputStream(data.getData());
// convert to bitmap with declared size.
Bitmap bitmap = BitmapFactory.decodeStream(in, null, options);
in.close();

Upvotes: 1

Related Questions