Android - 如何使用Intent数据获取图像
我实现了从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
在您的代码中执行以下操作:
这应该可行。
编辑:
另外,如果您想要“缩略图”,请执行以下操作:
Do the following in your code:
This should work.
Edit:
Also if you want a "thumbnail" do the following:
实现这一点的方法非常简单,只需:
记住在所有内容都首先初始化之后放置此代码,否则您将得到一个 NullPointerException 。我更喜欢把它放在
onCreate()
的底部Well the way go about doing this is very easy, just:
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 theonCreate()
这个问题可以通过编写更少的代码行来解决。
This problem can be solved by writing fewer lines of codes.
我有这个代码:
然后在 onActivityResult 中我做了这个测试:
它适用于我的 api level 7
I have this code:
and then in onActivityResult I make this test:
it works for me api level 7
如果您想从共享文件接收图像,请使用以下代码(Kotlin)
If you want to receive image from Sharing File, here the code (Kotlin)