Fix camera failure on Android 10
package com.google.appinventor.components.runtime;
+import android.os.Build;
+import android.support.v4.content.FileProvider;
import com.google.appinventor.components.annotations.DesignerComponent;
import com.google.appinventor.components.annotations.PropertyCategory;
import com.google.appinventor.components.annotations.SimpleEvent;
@@ -51,8 +53,8 @@ import java.util.Date;
public class Camera extends AndroidNonvisibleComponent
implements ActivityResultListener, Component {
- private static final String CAMERA_INTENT = "android.media.action.IMAGE_CAPTURE";
- private static final String CAMERA_OUTPUT = "output";
+ private static final String CAMERA_INTENT = MediaStore.ACTION_IMAGE_CAPTURE;
+ private static final String CAMERA_OUTPUT = MediaStore.EXTRA_OUTPUT;
private final ComponentContainer container;
private Uri imageFile;
@@ -131,9 +133,10 @@ public class Camera extends AndroidNonvisibleComponent
if (Environment.MEDIA_MOUNTED.equals(state)) {
Log.i("CameraComponent", "External storage is available and writable");
- imageFile = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
+ File image = new File(Environment.getExternalStorageDirectory(),
"/Pictures/app_inventor_" + new Date().getTime()
- + ".jpg"));
+ + ".jpg");
+ imageFile = Uri.fromFile(image);
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, imageFile.getPath());
@@ -144,8 +147,13 @@ public class Camera extends AndroidNonvisibleComponent
requestCode = form.registerForActivityResult(this);
}
- Uri imageUri = container.$context().getContentResolver().insert(
+ Uri imageUri;
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
+ imageUri = container.$context().getContentResolver().insert(
MediaStore.Images.Media.INTERNAL_CONTENT_URI, values);
+ } else {
+ imageUri = NougatUtil.getPackageUri(form, image);
+ }
Intent intent = new Intent(CAMERA_INTENT);
intent.putExtra(CAMERA_OUTPUT, imageUri);
