gpt4 book ai didi

java - 从手机图库中选择图片

转载 作者:行者123 更新时间:2023-11-30 18:56:36 25 4
gpt4 key购买 nike

关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。












想改进这个问题?将问题更新为 on-topic对于堆栈溢出。

7年前关闭。




Improve this question




这是我的日志猫
06-03 17:35:58.603: E/AndroidRuntime(300): FATAL EXCEPTION: main
06-03 17:35:58.603: E/AndroidRuntime(300): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/1 }} to activity {com.example.fatwallet/com.example.fatwallet.Create_adds_Activity}: java.lang.NullPointerException
06-03 17:35:58.603: E/AndroidRuntime(300): at android.app.ActivityThread.deliverResults(ActivityThread.java:3515)
06-03 17:35:58.603: E/AndroidRuntime(300): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3557)
06-03 17:35:58.603: E/AndroidRuntime(300): at android.app.ActivityThread.access$2800(ActivityThread.java:125)
06-03 17:35:58.603: E/AndroidRuntime(300): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2063)
06-03 17:35:58.603: E/AndroidRuntime(300): at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 17:35:58.603: E/AndroidRuntime(300): at android.os.Looper.loop(Looper.java:123)
06-03 17:35:58.603: E/AndroidRuntime(300): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-03 17:35:58.603: E/AndroidRuntime(300): at java.lang.reflect.Method.invokeNative(Native Method)
06-03 17:35:58.603: E/AndroidRuntime(300): at java.lang.reflect.Method.invoke(Method.java:521)
06-03 17:35:58.603: E/AndroidRuntime(300): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-03 17:35:58.603: E/AndroidRuntime(300): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-03 17:35:58.603: E/AndroidRuntime(300): at dalvik.system.NativeStart.main(Native Method)
06-03 17:35:58.603: E/AndroidRuntime(300): Caused by: java.lang.NullPointerException
06-03 17:35:58.603: E/AndroidRuntime(300): at com.example.fatwallet.Create_adds_Activity.onActivityResult(Create_adds_Activity.java:101)
06-03 17:35:58.603: E/AndroidRuntime(300): at android.app.Activity.dispatchActivityResult(Activity.java:3890)
06-03 17:35:58.603: E/AndroidRuntime(300): at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)

最佳答案

简单通过 Intent 优先

Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);

你会在你的onActivityResult上得到图片路径
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}

获取完整源代码 here

关于java - 从手机图库中选择图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24012412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com