我在两个图像视图上显示两个图像,一个带有请求代码101,另一个带有102
startActivityForResult(Intent.createChooser(intent, "Select Picture"), 101);// change request
startActivityForResult(Intent.createChooser(intent1, "Select Picture"), 102); // change request code
并且我想要他们的路径名称,例如,img1.jpg或路径,例如,storage / emulated / 0 / DCIM / camera / img1.jpg将显示在2个textviews上,我已经为我写了以下代码但是它是强制停止应用程序
if (requestCode == 101) {
Uri selectedImageUri = data.getData();
ImageView iv = (ImageView) findViewById(R.id.imgView);
File file = new File( getPathFromURI(selectedImageUri));
String path = file.getAbsolutePath();
iv.setImageBitmap(BitmapFactory.decodeFile(path));
iv.setTag(path);
String impath = (String) iv.getTag();
TextView myText = (TextView) findViewById(R.id.tv);
myText.setText(impath);
}
else if(requestCode == 102){
Uri selectedImageUri = data.getData();
ImageView iv = (ImageView) findViewById(R.id.imgView2);
File file = new File( getPathFromURI(selectedImageUri));
String path1 = file.getAbsolutePath();
iv.setImageBitmap(BitmapFactory.decodeFile(path1));
iv.setTag(path1);
String impath = (String) iv.getTag();
TextView myText1 = (TextView) findViewById(R.id.tv1);
myText1.setText(impath);
}
public String getPathFromURI(Uri contentUri) {
String res = null;
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
if (cursor.moveToFirst()) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}
cursor.close();
return res;
}
错误日志说,
Process: select_image_demo.coderzheaven.com.selectimage, PID: 11845
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=102, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:42038 flg=0x1 }} to activity {select_image_demo.coderzheaven.com.selectimage/select_image_demo.coderzheaven.com.selectimage.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.deliverResults(ActivityThread.java:4090)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4133)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1534)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
Caused by: java.lang.NullPointerException
at java.io.File.<init>(File.java:262)
at select_image_demo.coderzheaven.com.selectimage.MainActivity.onActivityResult(MainActivity.java:84)
at android.app.Activity.dispatchActivityResult(Activity.java:6979)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4086)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4133)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1534)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6121)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)