我正尝试从该活动的图库中获取图像后,将图像从另一个活动发送回主活动。应用程序在执行此操作时崩溃。我怎么解决这个问题?这是我的代码
第二项活动
Intent returnIntent = new Intent();
String str = filePath.toString();
returnIntent.putExtra("UPDATED_PIC", str);
setResult(RESULT_OK,returnIntent);
主要活动
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PROFILE_PICTURE_REQUEST) {
if (resultCode == RESULT_OK) {
if(data != null) {
String str = getIntent().getStringExtra("UPDATED_PIC");
//Getting Error here
filePath = Uri.parse(str);
try {
//Getting the Bitmap from Gallery
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
// Setting the Bitmap to ImageView
mNetworkImageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
错误记录
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.profilepicture, PID: 4975
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { (has extras) }} to activity {com.example.admin.profilepicture/com.example.admin.profilepicture.MainActivity}: java.lang.NullPointerException: uriString
at android.app.ActivityThread.deliverResults(ActivityThread.java:4089)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: uriString
at android.net.Uri$StringUri.<init>(Uri.java:475)
at android.net.Uri$StringUri.<init>(Uri.java)
at android.net.Uri.parse(Uri.java:437)
at com.example.admin.profilepicture.MainActivity.onActivityResult(MainActivity.java:61)
at android.app.Activity.dispatchActivityResult(Activity.java:6932)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4085)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
答案 0 :(得分:2)
您需要在onActivityResult()
MainActivity
上编写此代码
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
String str = data.getStringExtra("UPDATED_PIC");
filePath = Uri.parse(str);
}
使用data
代替getIntent()
onActivityResult()
在调用SecondActivity时,您也应该传递请求代码
查看this参考问题
答案 1 :(得分:1)
而不是getIntent()
尝试使用data.getStringExtra()
onActivityResult()