Android get image from gallery into ImageView(Android 从图库中获取图像到 ImageView)
问题描述
我正在尝试将照片从画廊添加到 ImageView
但我收到此错误:
I'm trying to add a photo from galery to a ImageView
but I get this error:
java.lang.RuntimeException: 传递结果失败ResultInfo{who=null, request=1, result=-1, data=Intent {dat=content://media/external/images/media/1 }} 到活动{hotMetter.pack/hotMetter.pack.GetPhoto}:java.lang.NullPointerException
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/1 }} to activity {hotMetter.pack/hotMetter.pack.GetPhoto}: java.lang.NullPointerException
这是我的代码:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
Bitmap bitmap=null;
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == Activity.RESULT_OK)
{
if (requestCode == SELECT_PICTURE)
{
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
tv.setText(selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
public String getPath(Uri uri)
{
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor == null) return null;
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String s=cursor.getString(column_index);
cursor.close();
return s;
}
我得到 selectedImagePath="mnt/sdcard/DCIM/myimage"
但在 img.setImageURI(selectedImageUri);
我得到错误.
I get the selectedImagePath="mnt/sdcard/DCIM/myimage"
but on img.setImageURI(selectedImageUri);
i get the error.
我也使用了 Bitmap
并尝试从 SetImageBitmap
设置图像,但我得到了同样的错误.
I've also used a Bitmap
and tried to set the image from SetImageBitmap
but i get the same error.
LogCat:
05-06 19:41:34.191: E/AndroidRuntime(8466): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/1 }} to activity {hotMetter.pack/hotMetter.pack.GetPhoto}: java.lang.NullPointerException
05-06 19:41:34.191: E/AndroidRuntime(8466): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
05-06 19:41:34.191: E/AndroidRuntime(8466): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2574)
05-06 19:41:34.191: E/AndroidRuntime(8466): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
05-06 19:41:34.191: E/AndroidRuntime(8466): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:961)
05-06 19:41:34.191: E/AndroidRuntime(8466): at android.os.Handler.dispatchMessage(Handler.java:99)
05-06 19:41:34.191: E/AndroidRuntime(8466): at android.os.Looper.loop(Looper.java:123)
05-06 19:41:34.191: E/AndroidRuntime(8466): at android.app.ActivityThread.main(ActivityThread.java:3683)
05-06 19:41:34.191: E/AndroidRuntime(8466): at java.lang.reflect.Method.invokeNative(Native Method)
05-06 19:41:34.191: E/AndroidRuntime(8466): at java.lang.reflect.Method.invoke(Method.java:507)
05-06 19:41:34.191: E/AndroidRuntime(8466): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-06 19:41:34.191: E/AndroidRuntime(8466): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-06 19:41:34.191: E/AndroidRuntime(8466): at dalvik.system.NativeStart.main(Native Method)
05-06 19:41:34.191: E/AndroidRuntime(8466): Caused by: java.lang.NullPointerException
05-06 19:41:34.191: E/AndroidRuntime(8466): at hotMetter.pack.GetPhoto.onActivityResult(GetPhoto.java:55)
05-06 19:41:34.191: E/AndroidRuntime(8466): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
05-06 19:41:34.191: E/AndroidRuntime(8466): at android.app.ActivityThread.deliverResults(ActivityThread.java:2528)
请提供建议.谢谢!
推荐答案
在调试模式下运行应用程序并在 if (requestCode == SELECT_PICTURE)
上设置断点,并在逐步执行时检查每个变量以确保它按预期设置.如果您在 img.setImageURI(selectedImageUri);
上获得 NPE,则未设置 img
或 selectedImageUri
.
Run the app in debug mode and set a breakpoint on if (requestCode == SELECT_PICTURE)
and inspect each variable as you step through to ensure it is being set as expected. If you are getting a NPE on img.setImageURI(selectedImageUri);
then either img
or selectedImageUri
are not set.
这篇关于Android 从图库中获取图像到 ImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Android 从图库中获取图像到 ImageView
基础教程推荐
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01