Android - 如何使用Intent数据获取图像

发布于 2024-12-11 03:00:35 字数 1489 浏览 0 评论 0原文

我实现了从 SD卡相册中获取图像的应用程序。但它无法正常工作。

这里 Intent 返回如下 Intent { act=com.htc.HTCAlbum.action.ITEM_PICKER_FROM_COLLECTIONS dat=content://media/external/images/media/9typ=image/jpeg (has extras) }

代码中

位图缩略图 = (Bitmap) data.getExtras().get("data");
这里(位图)data.getExtras().get(“data”) 这部分返回null

如何在这里获取位图请任何人帮助我。

代码:

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

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

离旧人 2024-12-18 03:00:35

在您的代码中执行以下操作:

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

这应该可行。

编辑:
另外,如果您想要“缩略图”,请执行以下操作:

Bitmap bitmap = MediaStore.Images.Thumbnails.getThumbnail(
                    getContentResolver(), selectedImageUriId,
                    MediaStore.Images.Thumbnails.MICRO_KIND,
                    (BitmapFactory.Options) null);

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);
时光暖心i 2024-12-18 03:00:35

实现这一点的方法非常简单,只需:

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

记住在所有内容都首先初始化之后放置此代码,否则您将得到一个 NullPointerException 。我更喜欢把它放在 onCreate() 的底部

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

凝望流年 2024-12-18 03:00:35

这个问题可以通过编写更少的代码行来解决。

 if(requestCode== your_request_code){
            if(resultCode==RESULT_OK){
                Uri uri = data.getData();  //<----get the image uri
                imageView.setImageURI(uri); //<----set the image uri
  }

This problem can be solved by writing fewer lines of codes.

 if(requestCode== your_request_code){
            if(resultCode==RESULT_OK){
                Uri uri = data.getData();  //<----get the image uri
                imageView.setImageURI(uri); //<----set the image uri
  }
在巴黎塔顶看东京樱花 2024-12-18 03:00:35

我有这个代码:

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

然后在 onActivityResult 中我做了这个测试:

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

它适用于我的 api level 7

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

得不到的就毁灭 2024-12-18 03:00:35

如果您想从共享文件接收图像,请使用以下代码(Kotlin)

if (intent.type?.startsWith("image/") == true) {
            (intent.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let {

            }
        }
}

If you want to receive image from Sharing File, here the code (Kotlin)

if (intent.type?.startsWith("image/") == true) {
            (intent.getParcelableExtra<Parcelable>(Intent.EXTRA_STREAM) as? Uri)?.let {

            }
        }
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文