2015-09-27 41 views
-1

我写了下面的代码onActivityResult方法调用CropImage.class这将给裁剪后的图像,但我同时从作物的活动返回Add_Customer活动的Android onActivityResult错误

调用CropImage活动与经过3中面临的一个错误下面的方法:

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 
     Toast.makeText(getApplicationContext(), "Called Succesfully..." + requestCode, Toast.LENGTH_SHORT).show(); 
     if (requestCode == 1) 
     { 
      Intent intent = new Intent(Add_Customer.this,CropImage.class); 
      startActivityForResult(intent, 3); 
     } 

     if (requestCode ==3) 
     { 
      Toast.makeText(getApplicationContext(),"Photo Added Succesfully...", Toast.LENGTH_SHORT).show(); 
      File f = new File(Environment.DIRECTORY_PICTURES); 
      for (File temp : f.listFiles()) { 
       if (temp.getName().equals("temp.jpg")) { 
        f = temp; 
        break; 
       } 
      } 
      BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); 

      Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath(), 
        bitmapOptions); 

      viewImage.setImageBitmap(bmp); 
     } 

    } 

CropImage活动的onCreate:

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_crop_image); 

final CropImageView cropImageView = (CropImageView)findViewById(R.id.cropImageView); 
final ImageView croppedImageView = (ImageView)findViewById(R.id.croppedImageView); 
     // Set image for cropping 

     File f = new File(Environment.getExternalStorageDirectory().toString()); 
     for (File temp : f.listFiles()) 
     { 
      if (temp.getName().equals("temp.jpg")) 
      { 
      f = temp; 
      break; 
      } 
     } 
     try 
     { 

     BitmapFactory.Options bitmapOptions = new BitmapFactory.Options(); 

     bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(), 
     bitmapOptions); 

     cropImageView.setImageBitmap(bitmap); 



     //deleting image captured by camera 
     f.delete(); 


     } catch (Exception e) { 
     e.printStackTrace(); 
     } 




     Button cropButton = (Button)findViewById(R.id.crop_button); 
     cropButton.setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) { 
     // Get cropped image, and show result. 
     croppedImageView.setImageBitmap(cropImageView.getCroppedBitmap()); 

     ByteArrayOutputStream bStream = new ByteArrayOutputStream(); 
     Bitmap crop = cropImageView.getCroppedBitmap(); 

     OutputStream outFile = null; 
     File file = new File(Environment.DIRECTORY_PICTURES, "temp.jpg"); 
     try { 
     outFile = new FileOutputStream(file); 
     crop.compress(Bitmap.CompressFormat.JPEG, 85, outFile); 
     outFile.flush(); 
     outFile.close(); 

     } catch (Exception e) { 
     e.printStackTrace(); 
     } 


     Intent intentMessage = new Intent(); 

     // put the message in Intent 
     intentMessage.putExtra("MESSAGE", "hello"); 
     intentMessage.putExtra("image", crop); 
     setResult(3, intentMessage); 
     // finish The activity 
     finish(); 
     } 
     }); 
     } 

错误:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=3, result=3, data=Intent { (has extras) }} to activity {loginscreen.example.com.girviapp/loginscreen.example.com.girviapp.Add_Customer}: java.lang.NullPointerException: Attempt to get length of null array 
      at android.app.ActivityThread.deliverResults(ActivityThread.java:3539) 
      at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582) 
      at android.app.ActivityThread.access$1300(ActivityThread.java:144) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5221) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
    Caused by: java.lang.NullPointerException: Attempt to get length of null array 
      at loginscreen.example.com.girviapp.Add_Customer.onActivityResult(Add_Customer.java:196) 
      at android.app.Activity.dispatchActivityResult(Activity.java:6135) 
      at android.app.ActivityThread.deliverResults(ActivityThread.java:3535) 
      at android.app.ActivityThread.handleSendResult(ActivityThread.java:3582) 
      at android.app.ActivityThread.access$1300(ActivityThread.java:144) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1327) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5221) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

回答

0

您的回调意图没有传递正确的resultCode,并且这是因为它的崩溃。

更改您的代码CropImage活动中的onClick)以下(:

Intent intentMessage = new Intent(); 

    // put the message in Intent 
    intentMessage.putExtra("MESSAGE", "hello"); 
    intentMessage.putExtra("image", crop); 
    setResult(RESULT_OK, intentMessage); 
    // finish The activity 
    finish(); 
+0

但是我应该在哪里通过3然后 –

+0

你不需要通过。 Android记住您已通过“startActivityForResult(intent,3)”的请求代码;“ –

+0

更改后的eRRor: java.lang.RuntimeException:未将结果resultInfo {who = null,request = 3,result = -1,data = Intent {(has extras)}}传递给活动 –

0

我在onActivityResult取得使用getIntent.getByteArrayExtra( “图像”),而不是data.getByteArrayExtra( “图像”)的愚蠢的错误方法