Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to get the image using Intent data

I implemented the application for getting image from the camera album in sdcard.But it is not working properly.

Here Intent returns like this Intent { act=com.htc.HTCAlbum.action.ITEM_PICKER_FROM_COLLECTIONS dat=content://media/external/images/media/9 typ=image/jpeg (has extras) }

In the code

Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
Here (Bitmap) data.getExtras().get("data") this part returns null.

How to get the bitmap here please can anybody help me.

Code:

cam_images_btn.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {

             Intent cam_ImagesIntent = new Intent(Intent.ACTION_GET_CONTENT);
             cam_ImagesIntent.setType("image/*");
             startActivityForResult(cam_ImagesIntent, CAMERA_IMAGES_REQUEST); 
        }       
    }); 

    if(requestCode == CAMERA_IMAGES_REQUEST && resultCode==Activity.RESULT_OK)
    {
        System.out.println("data(CAMERA_IMAGES_REQUEST):"+data);
        if(data != null)
        {           
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data"); 
            System.out.println("Bitmap(CAMERA_IMAGES_REQUEST):"+thumbnail);
            System.out.println("cap_image(CAMERA_IMAGES_REQUEST):"+cap_image);
            cap_image.setImageBitmap(thumbnail); 
        }
        else
        {
            System.out.println("SDCard have no images");
            Toast.makeText(camera.this, "SDCard have no images", Toast.LENGTH_SHORT);        
        }
    }

thanks

like image 405
naresh Avatar asked Oct 20 '11 07:10

naresh


2 Answers

Do the following in your code:

 if(data != null)
    {           
        Uri selectedImageUri = data.getData();
        filestring = selectedImageUri.getPath();

      Bitmap thumbnail = BitmapFactory.decodeFile(filestring, options2);

        System.out.println("Bitmap(CAMERA_IMAGES_REQUEST):"+thumbnail);
        System.out.println("cap_image(CAMERA_IMAGES_REQUEST):"+cap_image);
        cap_image.setImageBitmap(thumbnail); 
    }

This should work.

Edit: Also if you want a "thumbnail" do the following:

Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(
                    getContentResolver(), selectedImageUriId,
                    MediaStore.Images.Thumbnails.MICRO_KIND,
                    (BitmapFactory.Options) null);
like image 166
Gaurav Navgire Avatar answered Oct 14 '22 22:10

Gaurav Navgire


Well the way go about doing this is very easy, just:

        //Get incoming intent
        Intent intent = getIntent();
        intent.setType("image/*");
        String action = intent.getAction();
        String type = intent.getType();

        if(Intent.ACTION_SEND.equals(action) && type != null){
            handleIncomingData(intent);
        }


    public void handleIncomingData(Intent data){
        Uri imageSelected = data.getParcelableExtra(Intent.EXTRA_STREAM);
        try{
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageSelected);
            imageView.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

Remember to put this code after everything is initialized first or else you will get a NullPointerException. I prefer to put it at the bottom of the onCreate()

like image 37
Yusuph wickama Avatar answered Oct 14 '22 22:10

Yusuph wickama