Skip to content

Commit

Permalink
Android: Save Photos to Library on Android 10+ (#737)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottrules44 authored Oct 31, 2024
1 parent c9c11da commit fb2a24d
Showing 1 changed file with 41 additions and 16 deletions.
57 changes: 41 additions & 16 deletions platform/android/sdk/src/com/ansca/corona/NativeToJavaBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@
import java.security.PublicKey;
import java.security.Signature;
import java.security.spec.X509EncodedKeySpec;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.net.URL;
import java.net.MalformedURLException;

import android.app.Activity;
import android.content.ContentValues;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.graphics.Bitmap;
Expand All @@ -43,8 +47,10 @@
import android.media.MediaScannerConnection;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.location.Location;
import android.provider.MediaStore;
import android.util.Base64;
import android.util.Log;
import android.view.DisplayCutout;
Expand Down Expand Up @@ -1096,20 +1102,10 @@ else if ((fileExtensionName.length() > 0) && (fileExtensionName.startsWith(".")
String applicationName = CoronaEnvironment.getApplicationName();

// Create a unique file name in the directory.
java.io.File uniqueFile = null;
try {
for (int index = 1; index <= 10000; index++) {
String fileName = applicationName + " Picture " + Integer.toString(index) + fileExtensionName;
java.io.File nextFile = new java.io.File(directory, fileName);
if (nextFile.exists() == false) {
uniqueFile = nextFile;
break;
}
}
}
catch (Exception ex) {
ex.printStackTrace();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
String currentTimeStamp = sdf.format(Calendar.getInstance().getTime());
String fileName = applicationName + " Picture " + currentTimeStamp + fileExtensionName;
java.io.File uniqueFile = new java.io.File(directory, fileName);
return uniqueFile;
}

Expand Down Expand Up @@ -1182,6 +1178,7 @@ protected static boolean callSaveBitmap( CoronaRuntime runtime, int[] pixels, in
}
filePathName = newFile.getPath();
addToPhotoLibrary = true;

}

// Copy the pixel array into a bitmap object.
Expand All @@ -1199,7 +1196,6 @@ protected static boolean callSaveBitmap( CoronaRuntime runtime, int[] pixels, in

SaveBitmapRequestPermissionsResultHandler resultHandler = new SaveBitmapRequestPermissionsResultHandler(
runtime, bitmap, quality, filePathName, addToPhotoLibrary);

return resultHandler.handleSaveMedia();
}

Expand Down Expand Up @@ -1287,7 +1283,36 @@ public SaveBitmapRequestPermissionsResultHandler(
public boolean handleSaveMedia() {
// We only check for External storage permission if the user wants to add to the Photo library.
if (fAddToPhotoLibrary) {
return super.handleSaveMedia();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { // Requires use to Media Store to Save on Android 10+
ContentValues values = new ContentValues();
String fileName = fFilePathName.substring(fFilePathName.lastIndexOf('/') + 1);
values.put(MediaStore.Images.Media.DISPLAY_NAME, fileName);
if(fileName.contains(".png")){
values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
}else{
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
}
values.put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES);
values.put(MediaStore.Images.Media.IS_PENDING, 1);

try {
Uri uri = CoronaEnvironment.getApplicationContext().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
if (uri != null) {
try (OutputStream out = CoronaEnvironment.getApplicationContext().getContentResolver().openOutputStream(uri)) {
fBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
}
values.clear();
values.put(MediaStore.Images.Media.IS_PENDING, 0);
CoronaEnvironment.getApplicationContext().getContentResolver().update(uri, values, null, null);
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
}
else{
return super.handleSaveMedia();
}
}
return executeSaveMedia();
}
Expand Down

0 comments on commit fb2a24d

Please sign in to comment.