Android - How to get the image using Intent data

2019-03-31 07:52发布

问题:

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

回答1:

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);


回答2:

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()



回答3:

I have this code:

public void onGalleryRequest() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(
            Intent.createChooser(intent,
                        getResources().getString(R.string.selectImage)),
            GALLERY_REQ);
}

and then in onActivityResult I make this test:

if (requestCode == CAMERA_PIC_REQUEST && data != null
            && resultCode != 0)

it works for me api level 7