且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

启动活动的结果不工作

更新时间:2023-01-24 07:43:32

开始活动

 私有静态最终诠释PICK_IMAGE = 1;
意向意图=新的意图();
intent.setType(图像/ *);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(意向,选择图片),PICK_IMAGE);
 

取结果

  @覆盖
  保护无效onActivityResult(INT申请code,INT结果code,意图数据)
  {
    如果(要求code == PICK_IMAGE和放大器;&放大器;数据= NULL和放大器;!&安培;!data.getData()= NULL)
     {
       乌里_uri = data.getData();
       //用户必须选择一个图像。
       光标光标= getContentResolver()查询(_uri,新的String [] {
       android.provider.MediaStore.Images.ImageColumns.DATA},NULL,NULL,NULL);
       cursor.moveToFirst();

       //链接到图片
       最后弦乐imageFilePath = cursor.getString(0);

       cursor.close();
     }
    super.onActivityResult(要求code,因此code,数据);
 

}

I'm using start Activity result for starting a new activity to select a image from gallery and it will return a image path to my main activity to , so that it will insert image in my main activity,

here is my code

    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;
    }
}

but i'm getting following crashes

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

Start Activity

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);

Take Result

 @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);

}