启动活动以使结果无效

时间:2014-11-04 06:09:25

标签: java android android-activity

我正在使用启动活动结果来启动新活动以从库中选择图像,它将返回我的主要活动的图像路径,以便它将在我的主要活动中插入图像,

这是我的代码

    Intent intent = new Intent(getApplicationContext(), Image.class);
    intent.putExtra(UUID, image.getUuid().toString());
    startActivityForResult(intent, PICK_IMAGE);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {

    case PICK_IMAGE:
        if (resultCode != RESULT_OK)
            return;
        String uuidStr = data.getStringExtra(UUID);
        log.v("image url",uuiStr);            
        break;
    }
}

但是我遇到了崩溃事件

java.lang.RuntimeException: Unable to resume activity 
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, 
result=-1,data=Intent { (has extras) }} to activity       
{com.write.example/com.write.example.MainWriterActivity}: java.lang.NullPointerException

2 个答案:

答案 0 :(得分:1)

意图是错误的,试试这个:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");

答案 1 :(得分:1)

开始活动

private static final int PICK_IMAGE = 1;
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);

取得结果

 @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) 
  {
    if(requestCode == PICK_IMAGE && data != null && data.getData() != null)
     {
       Uri _uri = data.getData();
       //User had pick an image.
       Cursor cursor = getContentResolver().query(_uri, new String[] {     
       android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
       cursor.moveToFirst();

       //Link to the image
       final String imageFilePath = cursor.getString(0);

       cursor.close();
     }
    super.onActivityResult(requestCode, resultCode, data);

}