Fehler liefert Ergebnis ResultInfo{wer=null, request=2, Ergebnis=-1, data=Absicht { }} zur Aktivität
Habe ich eine Methode, die den Nutzern die option, um die Kamera-Vorsatz zu erfassen, ein Foto an, dann hochladen von der Anwendung:
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 eine Kamera ein Bild aufgenommen habe ich das Bild um die Größe zu reduzieren, bevor es tatsächlich hochgeladen. Dies ist, wo bekomme ich einen crash.
@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 bin-Skalierung das Bild der Kamera nach unten, aber es ist nicht so offensichtlich aus meinem stack-trace. Wenn ich entfernen Sie die zwei Zeilen Quelltext verantwortlich für die umskalierung des Bildes, bekomme ich die 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 könnte schief gehen?
Du musst angemeldet sein, um einen Kommentar abzugeben.
ACTION_IMAGE_CAPTURE
nicht wieder eineUri
. Sie kennen dieUri
wo das Bild sein soll — Sie setzen es inEXTRA_OUTPUT
. Sie müssen festhalten, dass die Lage (auch über die gespeicherte Instanz StaatBundle
) und dort zu suchen.Beachten Sie auch, dass
Uri.fromFile()
funktioniert nicht auf Android 7.0+, sobald Sie heben Sie IhretargetSdkVersion
auf 25 oder höher.Diese Beispiel-app veranschaulicht die Verwendung
ACTION_IMAGE_CAPTURE
, einschließlich der Verwendung vonFileProvider
zu vermeidenUri.fromFile()
.