2017-03-11 14 views
0

Ich habe eine Methode, die Benutzern die Möglichkeit gibt, die Kameraabsicht zu verwenden, um ein Foto zum Hochladen zu erfassen zur Anwendung:Fehler beim Liefern von Ergebnis ResultInfo {who = null, request = 2, result = -1, data = Intent {}} zu Aktivität

public void UploadImageToFeed() { 

     CharSequence colors[] = new CharSequence[] {"Gallery", "Take a Picture"}; 

     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Upload an Image"); 
     builder.setIcon(R.drawable.ic_upload_image); 
     builder.setItems(colors, (dialog, which) -> { 
      if (which == 0) { 
       Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); 
       photoPickerIntent.setType("image/*"); 
       startActivityForResult(photoPickerIntent, SELECT_PHOTO); 
      } else { 
       Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
       File file = new File(Environment.getExternalStorageDirectory(), "camera.jpg"); 
       Uri uri = Uri.fromFile(file); 
       captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri); 
       startActivityForResult(captureIntent, SELECT_PHOTO); 
      } 
     }); 
     builder.show(); 

    } 

Wenn ein Kamerabild aufgenommen wird, verarbeite ich das Bild seine Größe zu reduzieren, bevor es tatsächlich hochgeladen. Hier bekomme ich einen Absturz.

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { 
     super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

     switch (requestCode) { 
      case SELECT_PHOTO: 
       if (resultCode == RESULT_OK) { 

        ImageView imageToUpload = (ImageView) findViewById(R.id.imageToUpload); 
        imageToUpload.setVisibility(View.VISIBLE); 

        Uri selectedImage = imageReturnedIntent.getData(); 
        InputStream imageStream = null; 
        try { 
         imageStream = getContentResolver().openInputStream(selectedImage); 
        } catch (FileNotFoundException e) { 
         e.printStackTrace(); 
        } 
        Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream); 
        int nh = (int) (yourSelectedImage.getHeight() * (512.0/yourSelectedImage.getWidth())); 
        Bitmap scaled = Bitmap.createScaledBitmap(yourSelectedImage, 512, nh, true); 
        imageToUpload.setImageBitmap(scaled); 

       } 
     } 
    } 

Ausnahme:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { dat=content://com.google.android.apps.photos.contentprovider/-1/1/content://media/external/images/media/197149/ORIGINAL/NONE/724456777 flg=0x1 clip={text/uri-list U:content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F197149/ORIGINAL/NONE/724456777} }} to activity {com.yitter.android/com.yitter.android.activity.YeetActivity}: java.lang.IllegalArgumentException: width and height must be > 0 


at android.app.ActivityThread.deliverResults(ActivityThread.java:3699) 
                     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 
                     at android.app.ActivityThread.-wrap16(ActivityThread.java) 
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                     at android.os.Looper.loop(Looper.java:148) 
                     at android.app.ActivityThread.main(ActivityThread.java:5417) 
                     at java.lang.reflect.Method.invoke(Native Method) 
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                    Caused by: java.lang.IllegalArgumentException: width and height must be > 0 
                     at android.graphics.Bitmap.createBitmap(Bitmap.java:829) 
                     at android.graphics.Bitmap.createBitmap(Bitmap.java:808) 
                     at android.graphics.Bitmap.createBitmap(Bitmap.java:739) 
                     at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:615) 
                     at com.yitter.android.activity.YeetActivity$override.onActivityResult(YeetActivity.java:350) 
                     at com.yitter.android.activity.YeetActivity$override.access$dispatch(YeetActivity.java) 
                     at com.yitter.android.activity.YeetActivity.onActivityResult(YeetActivity.java:0) 
                     at android.app.Activity.dispatchActivityResult(Activity.java:6456) 
                     at android.app.ActivityThread.deliverResults(ActivityThread.java:3695) 
                     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)  
                     at android.app.ActivityThread.-wrap16(ActivityThread.java)  
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)  
                     at android.os.Handler.dispatchMessage(Handler.java:102)  
                     at android.os.Looper.loop(Looper.java:148)  
                     at android.app.ActivityThread.main(ActivityThread.java:5417)  
                     at java.lang.reflect.Method.invoke(Native Method)  
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Mein erster Verdacht war, dass der Fehler auftritt, wo ich nach unten das Kamerabild Skalierung, aber es ist nicht so offensichtlich von meinem Stack-Trace. Als ich die beiden Codezeilen entfernen, die für das Bild neu zu skalieren, bekomme ich folgende Fehlermeldung:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { }} to activity {com.yitter.android/com.yitter.android.activity.YeetActivity}: java.lang.NullPointerException: uri 
                     at android.app.ActivityThread.deliverResults(ActivityThread.java:3699) 
                     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 
                     at android.app.ActivityThread.-wrap16(ActivityThread.java) 
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                     at android.os.Looper.loop(Looper.java:148) 
                     at android.app.ActivityThread.main(ActivityThread.java:5417) 
                     at java.lang.reflect.Method.invoke(Native Method) 
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
                    Caused by: java.lang.NullPointerException: uri 
                     at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:60) 
                     at android.content.ContentResolver.openInputStream(ContentResolver.java:645) 
                     at com.yitter.android.activity.YeetActivity.onActivityResult(YeetActivity.java:366) 
                     at android.app.Activity.dispatchActivityResult(Activity.java:6456) 
                     at android.app.ActivityThread.deliverResults(ActivityThread.java:3695) 
                     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)  
                     at android.app.ActivityThread.-wrap16(ActivityThread.java)  
                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)  
                     at android.os.Handler.dispatchMessage(Handler.java:102)  
                     at android.os.Looper.loop(Looper.java:148)  
                     at android.app.ActivityThread.main(ActivityThread.java:5417)  
                     at java.lang.reflect.Method.invoke(Native Method)  
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)  

Was falsch gehen könnte?

Antwort

2

ACTION_IMAGE_CAPTURE gibt keine Uri zurück. Sie kennen die Uri wo das Bild sein soll — Sie setzen es in EXTRA_OUTPUT. Sie müssen diese Position beibehalten (einschließlich über den gespeicherten Instanzstatus Bundle) und dort suchen.

Beachten Sie auch, dass Uri.fromFile() nicht auf Android 7.0+ funktioniert, sobald Sie Ihre targetSdkVersion auf 25 oder höher erhöhen.

This sample app demonstriert mit ACTION_IMAGE_CAPTURE, einschließlich FileProvider zu vermeiden Uri.fromFile().

Verwandte Themen