Skip to content

Commit

Permalink
Android: fix Camera for Android 10 and higher
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrys committed Jan 7, 2021
1 parent a5482c9 commit f925317
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 10 deletions.
3 changes: 3 additions & 0 deletions lib/commonAPI/mediacapture/ext.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ android:
manifest_changes:
- ext/platform/android/AndroidManifest_rhomobile.xml
- ext/platform/android/ApplicationCameraActivity.erb
- ext/platform/android/ApplicationFileProvider.erb
# maven_deps:
# - 'androidx.core:core:1.0.0'
adds: ext/platform/android/adds
exttype: rakefile

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="<%= @appPackageName %>.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/image_fileprovider_paths"></meta-data>
</provider>
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="mediacapture_images" path="Pictures/" />
</paths>
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import android.view.SurfaceHolder;
import android.database.Cursor;

//import androidx.core.content.FileProvider;
import android.support.v4.content.FileProvider;

import com.rhomobile.rhodes.Base64;
import com.rhomobile.rhodes.Logger;
import com.rhomobile.rhodes.RhodesActivity;
Expand Down Expand Up @@ -642,6 +645,29 @@ public void setPreviewTop(int previewTop, IMethodResult result) {
//WM only
}



String currentPhotoPath;

private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date(System.currentTimeMillis()));
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = RhodesActivity.getContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);

// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
return image;
}




@Override
public void takePicture(Map<String, String> propertyMap, IMethodResult result) {
if (!hasPermission()) {
Expand Down Expand Up @@ -713,15 +739,46 @@ public void takePicture(Map<String, String> propertyMap, IMethodResult result) {
Logger.T(TAG, "Output filePath: " + filePath);
}

fileUri = RhodesActivity.getContext().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
//fileUri = RhodesActivity.getContext().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);




File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Logger.E(TAG, "$$$ Failed to create temporary file !!!");
}
// Continue only if the File was successfully created
if (photoFile != null) {
fileUri = FileProvider.getUriForFile(RhodesActivity.getContext(),
RhodesActivity.getContext().getPackageName() + ".fileprovider",
photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
//startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}


actualPropertyMap.put("captureUri", fileUri.toString());
actualPropertyMap.put("captureUriFilePath", currentPhotoPath);
propertyMap.put("dataURI", "");
Logger.T(TAG, "Output fileUri: " + fileUri.toString());

// intent is null with MediaStore.EXTRA_OUTPUT so adding fileuri to map and get it with same key
// if instead of MediaStore.EXTRA_OUTPUT any other key is used then the bitmap is null though the file is getting created
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);




//Uri fff = Uri.fromFile(new File(filePath));
//intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
//intent.putExtra(MediaStore.EXTRA_OUTPUT, fff);



intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else if (outputFormat.equalsIgnoreCase("dataUri")) {}
} else {
intent = new Intent(ContextFactory.getUiContext(), CameraActivity.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -305,24 +305,31 @@ public void onActivityResult(RhodesActivity activity, int requestCode, int resul

if (getActualPropertyMap().get("dataURI") == null) {
Logger.T(TAG, "getActualPropertyMap().get(dataURI) == null");

try {
imgPath = getFilePath(curUri);
Logger.T(TAG, "curUri ["+curUri+"]");

String captureUriFilePath = getActualPropertyMap().get("captureUriFilePath");
if (captureUriFilePath != null) {
imgPath = captureUriFilePath;
}
catch (Exception e) {
e.printStackTrace();
else {
try {
imgPath = getFilePath(curUri);
}
catch (Exception e) {
e.printStackTrace();
}
}
Logger.T(TAG, "curUri ["+curUri+"]");
Logger.T(TAG, "imgPath ["+imgPath+"]");
//Utils.platformLog("################", "intent.getData() ["+intent.getData()+"]");
if (imgPath == null) {
Logger.T(TAG, "imgPath is null !");
try {
imgPath = getFilePath(intent.getData());
}
catch (Exception e) {
e.printStackTrace();
}
}
Logger.T(TAG, "imgPath ["+imgPath+"]");


if (fromGallery) {
Expand Down

0 comments on commit f925317

Please sign in to comment.