mirror of
https://codeberg.org/Starfish/Imagepipe.git
synced 2025-10-05 22:52:39 +02:00
fix files not saving properly on devices with api <= 29
This commit is contained in:
@@ -343,8 +343,6 @@ public class ImageReceiver extends Activity{
|
||||
*/
|
||||
private Boolean piping_was_already_launched = false;
|
||||
|
||||
private boolean useScopedStorage = false;
|
||||
|
||||
/*
|
||||
* Class to determine the selected painting tool
|
||||
*/
|
||||
@@ -456,6 +454,13 @@ public class ImageReceiver extends Activity{
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean UseScopedStorage(){
|
||||
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.Q) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
final AdapterView.OnItemSelectedListener scaleOptionsListener = new AdapterView.OnItemSelectedListener() {
|
||||
@Override
|
||||
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
|
||||
@@ -830,11 +835,6 @@ public class ImageReceiver extends Activity{
|
||||
// testing
|
||||
// int[] values = new int[]{8,5,3,9,6,4,77,8,5,33,4,5,66,22,1,0};
|
||||
// ImageFilters.quicksort(values,0,values.length-1);
|
||||
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.Q){
|
||||
useScopedStorage = true;
|
||||
} else {
|
||||
useScopedStorage = false;
|
||||
}
|
||||
// this is a hack for devices with a buggy media store
|
||||
// useScopedStorage = false;
|
||||
// In versionCode 39 and below the picture cache file suffix was .tmp, above it is the suffix of the image type.
|
||||
@@ -2390,7 +2390,7 @@ public class ImageReceiver extends Activity{
|
||||
if (ImagepipePreferences.autosave(this_context)){
|
||||
final ImageContainer imageContainer = saveImage(false);
|
||||
if (imageContainer != null) {
|
||||
if (useScopedStorage) {
|
||||
if (UseScopedStorage()) {
|
||||
// no need to scan images when using scoped storage
|
||||
multiplePipeList.add(imageContainer);
|
||||
savedImageContainer = imageContainer;
|
||||
@@ -2971,7 +2971,7 @@ public class ImageReceiver extends Activity{
|
||||
}
|
||||
|
||||
private boolean hasReadWriteStorageRuntimePermission(){
|
||||
if (useScopedStorage){
|
||||
if (UseScopedStorage()){
|
||||
if (android.os.Build.VERSION.SDK_INT >= 26) {
|
||||
if (android.os.Build.VERSION.SDK_INT >= 33) {
|
||||
// request only read external storage
|
||||
@@ -3041,7 +3041,7 @@ public class ImageReceiver extends Activity{
|
||||
}
|
||||
}
|
||||
}
|
||||
if ((useScopedStorage && hasReadStoragePermission) || (hasReadStoragePermission && hasWriteStoragePermission)) {
|
||||
if ((UseScopedStorage() && hasReadStoragePermission) || (hasReadStoragePermission && hasWriteStoragePermission)) {
|
||||
if (missing_permissions_task == MISSING_PERMISSIONS_TASK_sendImageUriIntent) {
|
||||
missing_permissions_task = 0;
|
||||
sendImageUriIntent();
|
||||
@@ -3071,7 +3071,7 @@ public class ImageReceiver extends Activity{
|
||||
@TargetApi(Build.VERSION_CODES.M)
|
||||
private void showPermissionsRationale(){
|
||||
AlertDialog.Builder builder = new AlertDialog.Builder(this);
|
||||
if (useScopedStorage){
|
||||
if (UseScopedStorage()){
|
||||
builder.setView(R.layout.dialogpermissionrationale2);
|
||||
} else {
|
||||
builder.setView(R.layout.dialogpermissionrationale);
|
||||
@@ -3102,7 +3102,7 @@ public class ImageReceiver extends Activity{
|
||||
ImageContainer imageContainer = saveImage(false);
|
||||
if (imageContainer != null){
|
||||
// when using scoped storage, file is already in the mediastore and needs not to be scanned.
|
||||
if (useScopedStorage){
|
||||
if (UseScopedStorage()){
|
||||
// store current uri data, because scanning is not necessary
|
||||
savedImageContainer = new ImageContainer(imageContainer);
|
||||
shareImage(imageContainer);
|
||||
@@ -3132,14 +3132,14 @@ public class ImageReceiver extends Activity{
|
||||
}
|
||||
ImageContainer targetImageContainer;
|
||||
if ((image_needs_to_be_saved) || (savedImageContainer==null)){
|
||||
if (useScopedStorage) {
|
||||
if (UseScopedStorage()) {
|
||||
targetImageContainer = ImageWriter.toMediaStore(context,originalImageContainer);
|
||||
} else {
|
||||
targetImageContainer = ImageWriter.toFile(context,originalImageContainer);
|
||||
}
|
||||
int compression_rate = ImagepipePreferences.getQuality(context);
|
||||
if (!ImageWriter.saveBitmapToUri(context,bitmap_image,targetImageContainer.uri,compression_rate,exifData)){
|
||||
Toast.makeText(this, R.string.toast_error_save_failed +" scoped storage: "+useScopedStorage, Toast.LENGTH_LONG).show();
|
||||
// Toast.makeText(this, R.string.toast_error_save_failed +" scoped storage: "+useScopedStorage+" error: "+, Toast.LENGTH_LONG).show();
|
||||
return null;
|
||||
}
|
||||
image_needs_to_be_saved = false;
|
||||
|
@@ -30,6 +30,8 @@ import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.os.Environment;
|
||||
import android.provider.MediaStore;
|
||||
import android.widget.Toast;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.OutputStream;
|
||||
import java.security.SecureRandom;
|
||||
@@ -124,8 +126,8 @@ public class ImageWriter {
|
||||
public static ImageContainer toFile(Context context, ImageContainer originalImageContainer){
|
||||
// File system_picure_directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
|
||||
// String target_directory_string = system_picure_directory.getAbsolutePath()+File.separator+context.getResources().getString(R.string.app_folder);
|
||||
String target_directory_string = ImagepipePreferences.getPath(context);
|
||||
File target_directory = new File(target_directory_string);
|
||||
// String target_directory_string = ImagepipePreferences.getPath(context);
|
||||
File target_directory = new File(ImagepipePreferences.getDirectoryPath(context));
|
||||
target_directory.mkdirs();
|
||||
String targetname = determineUnusedFilename(context,originalImageContainer,target_directory);
|
||||
File file = new File(target_directory,targetname);
|
||||
@@ -193,6 +195,17 @@ public class ImageWriter {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
// create imagepipe target directory and the "Pictures" folder if they do not exist.
|
||||
if (!ImageReceiver.UseScopedStorage()){
|
||||
File system_picture_directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
|
||||
String target_directory_string = system_picture_directory.getAbsolutePath()+File.separatorChar+context.getResources().getString(R.string.app_folder);
|
||||
File target_directory = new File(target_directory_string);
|
||||
if (target_directory.mkdirs()){
|
||||
Log.v("Target directory created: "+target_directory.toString());
|
||||
} else {
|
||||
Log.v("Target directory already exists: "+target_directory.toString());
|
||||
}
|
||||
}
|
||||
Log.v("Writing bitmap tu uri: "+targetUri.toString());
|
||||
OutputStream outputStream = context.getContentResolver().openOutputStream(targetUri);
|
||||
Bitmap bitmap_output = sourceBitmap.copy(Bitmap.Config.ARGB_8888,true);
|
||||
@@ -205,8 +218,11 @@ public class ImageWriter {
|
||||
} else {
|
||||
Log.v("No exif tags allowed.");
|
||||
}
|
||||
// throw new Exception("Just for testing!!!");
|
||||
} catch (Exception e) {
|
||||
Log.v("ERROR saving bitmap to uri: "+e.getMessage());
|
||||
Toast toast = Toast.makeText(context,context.getResources().getString(R.string.toast_error_save_failed) +" Error: "+e.getMessage(),Toast.LENGTH_LONG);
|
||||
toast.show();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -275,14 +291,14 @@ public class ImageWriter {
|
||||
}
|
||||
|
||||
public static ImageContainer toMediaStore(Context context, ImageContainer originalImageContainer){
|
||||
String filePath = Environment.DIRECTORY_PICTURES+ File.separator+ImagepipePreferences.getPath(context);
|
||||
String filePath = ImagepipePreferences.getDirectoryPath(context);
|
||||
String desiredName = determineUnusedStorename(context,originalImageContainer,true);
|
||||
Log.v("Adding saved image to the mediastore -> target path: "+filePath+", desired name: "+desiredName);
|
||||
final ContentValues contentValues = new ContentValues();
|
||||
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, desiredName);
|
||||
contentValues.put(MediaStore.MediaColumns.MIME_TYPE,getMimeType(context));
|
||||
// means use of scoped storage
|
||||
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.Q) {
|
||||
if (ImageReceiver.UseScopedStorage()) {
|
||||
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, filePath);
|
||||
}
|
||||
// insert the metadata
|
||||
@@ -326,14 +342,17 @@ public class ImageWriter {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getDefaultSavePath(Context context){
|
||||
// means use of scoped storage
|
||||
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.Q) {
|
||||
return Environment.DIRECTORY_PICTURES+ File.separator+context.getResources().getString(R.string.app_folder);
|
||||
public static String getSavePath(String directoryName) {
|
||||
if (ImageReceiver.UseScopedStorage()) {
|
||||
return Environment.DIRECTORY_PICTURES + File.separator + directoryName;
|
||||
} else {
|
||||
File system_picure_directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
|
||||
return system_picure_directory.getAbsolutePath()+File.separator+context.getResources().getString(R.string.app_folder);
|
||||
File system_picture_directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
|
||||
return system_picture_directory.getAbsolutePath() + File.separator + directoryName;
|
||||
}
|
||||
}
|
||||
|
||||
public static String getDefaultSavePath(Context context){
|
||||
return getSavePath(context.getResources().getString(R.string.app_folder));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -608,14 +608,17 @@ public class ImagepipePreferences {
|
||||
pref_editor.apply();
|
||||
}
|
||||
|
||||
public static String getPath(Context context){
|
||||
public static String getDirectoryName(Context context){
|
||||
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
String path = sharedPreferences.getString(PREF_PATH, ImageWriter.getDefaultSavePath(context));
|
||||
return path;
|
||||
return sharedPreferences.getString(PREF_PATH, context.getResources().getString(R.string.app_folder));
|
||||
}
|
||||
|
||||
public static Uri getPathUri(Context context){
|
||||
Uri uri = Uri.parse(getPath(context));
|
||||
public static String getDirectoryPath(Context context){
|
||||
return ImageWriter.getSavePath(getDirectoryName(context));
|
||||
}
|
||||
|
||||
public static Uri getDirectoryUri(Context context){
|
||||
Uri uri = Uri.parse(getDirectoryPath(context));
|
||||
return uri;
|
||||
}
|
||||
|
||||
|
@@ -141,7 +141,7 @@ public class Settings extends PreferenceActivity implements SharedPreferences.On
|
||||
}
|
||||
Preference pathPreference = (Preference) findPreference(ImagepipePreferences.PREF_PATH);
|
||||
if (pathPreference!=null){
|
||||
pathPreference.setSummary(context.getResources().getString(R.string.preference_path_summary)+" "+ImagepipePreferences.getPath(context));
|
||||
pathPreference.setSummary(context.getResources().getString(R.string.preference_path_summary)+" "+ImagepipePreferences.getDirectoryName(context));
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user