Android-如何使用Intent数据获取图像 [英] Android - How to get the image using Intent data

查看:98
本文介绍了Android-如何使用Intent数据获取图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了从sdcard中的相册中获取图像的应用程序,但无法正常工作.

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

此处意图这样返回意图{act = com.htc.HTCAlbum.action.ITEM_PICKER_FROM_COLLECTIONS dat = content://media/external/images/media/9 typ = image/jpeg(has额外内容)}

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

在代码中

位图缩略图=(位图)data.getExtras().get("data");
在这里(位图)data.getExtras().get(数据") 这部分返回 null .

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.

代码:

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

谢谢

推荐答案

在代码中执行以下操作:

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

这应该有效.

另外,如果您想要缩略图",请执行以下操作:

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

这篇关于Android-如何使用Intent数据获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆