Skip to content

Commit 1e536a0

Browse files
committed
fixed file access and exif editing
1 parent f6dfcca commit 1e536a0

File tree

7 files changed

+53
-44
lines changed

7 files changed

+53
-44
lines changed

app/release/app-release.apk

3.56 KB
Binary file not shown.

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44

55
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
66
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
7-
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
8-
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
7+
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
98
tools:ignore="ScopedStorage" />
109

1110
<application
12-
android:requestLegacyExternalStorage="true"
1311
android:allowBackup="true"
1412
android:dataExtractionRules="@xml/data_extraction_rules"
1513
android:fullBackupContent="@xml/backup_rules"

app/src/main/java/ru/tech/imageresizershrinker/resize_screen/MainActivity.kt

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import android.os.Build
99
import android.os.Bundle
1010
import android.os.Environment
1111
import android.provider.MediaStore
12-
import android.provider.Settings
1312
import androidx.activity.ComponentActivity
1413
import androidx.activity.compose.BackHandler
1514
import androidx.activity.compose.rememberLauncherForActivityResult
@@ -76,6 +75,7 @@ import ru.tech.imageresizershrinker.theme.Github
7675
import ru.tech.imageresizershrinker.theme.Telegram
7776
import ru.tech.imageresizershrinker.utils.BitmapUtils
7877
import ru.tech.imageresizershrinker.utils.BitmapUtils.decodeBitmapFromUri
78+
import ru.tech.imageresizershrinker.utils.BitmapUtils.getUriByName
7979
import ru.tech.imageresizershrinker.utils.BitmapUtils.toMap
8080
import java.io.File
8181

@@ -168,6 +168,11 @@ class MainActivity : ComponentActivity() {
168168
Environment.DIRECTORY_DCIM
169169
), "ResizedImages"
170170
)
171+
},
172+
getFileDescriptor = { name ->
173+
getUriByName(name)?.let {
174+
contentResolver.openFileDescriptor(it, "rw", null)
175+
}
171176
}
172177
) { success ->
173178
if (!success) requestPermission()
@@ -914,33 +919,6 @@ class MainActivity : ComponentActivity() {
914919
}
915920
}
916921
)
917-
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
918-
var showDialog by remember(
919-
!Environment.isExternalStorageManager()
920-
) { mutableStateOf(!Environment.isExternalStorageManager()) }
921-
922-
if (showDialog) {
923-
AlertDialog(
924-
icon = { Icon(Icons.Rounded.Folder, null) },
925-
title = { Text(stringResource(R.string.memory)) },
926-
text = { Text(stringResource(R.string.memory_text)) },
927-
onDismissRequest = {},
928-
confirmButton = {
929-
Button(onClick = { startActivity(Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION)) }) {
930-
Text(stringResource(R.string.grant))
931-
}
932-
},
933-
dismissButton = {
934-
FilledTonalButton(
935-
onClick = {
936-
showDialog = !Environment.isExternalStorageManager()
937-
}
938-
) {
939-
Text(stringResource(R.string.done))
940-
}
941-
}
942-
)
943-
}
944922
}
945923

946924
ToastHost(hostState = toastHostState)

app/src/main/java/ru/tech/imageresizershrinker/resize_screen/viewModel/MainViewModel.kt

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package ru.tech.imageresizershrinker.resize_screen.viewModel
33
import android.graphics.Bitmap
44
import android.graphics.BitmapFactory
55
import android.os.Build
6+
import android.os.ParcelFileDescriptor
67
import androidx.compose.runtime.MutableState
78
import androidx.compose.runtime.getValue
89
import androidx.compose.runtime.mutableStateOf
@@ -61,6 +62,7 @@ class MainViewModel : ViewModel() {
6162
bitmap: Bitmap? = _bitmap.value,
6263
isExternalStorageWritable: Boolean,
6364
getFileOutputStream: (name: String, ext: String) -> OutputStream?,
65+
getFileDescriptor: (name: String) -> ParcelFileDescriptor?,
6466
getExternalStorageDir: () -> File?,
6567
onSuccess: (Boolean) -> Unit
6668
) = viewModelScope.launch {
@@ -109,11 +111,21 @@ class MainViewModel : ViewModel() {
109111
fos!!.flush()
110112
fos.close()
111113

112-
val dir = getExternalStorageDir()
113-
val image = File(dir, name)
114-
val ex = ExifInterface(image)
115-
exif?.copyTo(ex)
116-
ex.saveAttributes()
114+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
115+
val fd = getFileDescriptor(name)
116+
fd?.fileDescriptor?.let {
117+
val ex = ExifInterface(it)
118+
exif?.copyTo(ex)
119+
ex.saveAttributes()
120+
}
121+
fd?.close()
122+
} else {
123+
val dir = getExternalStorageDir()
124+
val image = File(dir, name)
125+
val ex = ExifInterface(image)
126+
exif?.copyTo(ex)
127+
ex.saveAttributes()
128+
}
117129

118130
_bitmap.value = decoded
119131
_bitmapInfo.value = _bitmapInfo.value.copy(

app/src/main/java/ru/tech/imageresizershrinker/utils/BitmapUtils.kt

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
package ru.tech.imageresizershrinker.utils
22

33
import android.content.ContentResolver
4+
import android.content.ContentUris
45
import android.content.Context
56
import android.graphics.Bitmap
67
import android.graphics.BitmapFactory
78
import android.graphics.Matrix
89
import android.graphics.Rect
910
import android.net.Uri
11+
import android.os.Build
1012
import android.os.ParcelFileDescriptor
13+
import android.provider.MediaStore
1114
import android.webkit.MimeTypeMap
15+
import androidx.annotation.RequiresApi
1216
import androidx.exifinterface.media.ExifInterface
1317
import java.io.ByteArrayInputStream
1418
import java.io.ByteArrayOutputStream
@@ -285,4 +289,29 @@ object BitmapUtils {
285289
return hashMap
286290
}
287291

292+
@RequiresApi(Build.VERSION_CODES.Q)
293+
fun Context.getUriByName(fileName: String?): Uri? {
294+
applicationContext.contentResolver.query(
295+
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
296+
arrayOf(MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media._ID),
297+
null, null
298+
)?.use { cursor ->
299+
while (cursor.moveToNext()) {
300+
val index =
301+
cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME).takeIf { it != -1 }
302+
?: 0
303+
val name = cursor.getString(index)
304+
if (name == fileName) {
305+
val pIndex = cursor.getColumnIndex(MediaStore.Images.Media._ID)
306+
.takeIf { it != -1 } ?: 0
307+
return ContentUris.withAppendedId(
308+
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
309+
cursor.getLong(pIndex)
310+
)
311+
}
312+
}
313+
}
314+
return null
315+
}
316+
288317
}

app/src/main/res/values-ru/strings.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,4 @@
3131
<string name="add_tag">Добавить тег</string>
3232
<string name="might_be_error_with_exif">Если выбран формат сжатия не JPEG, то данные EXIF могут не отображаться в других приложениях</string>
3333
<string name="save">Сохранить</string>
34-
<string name="grant">Предоставить</string>
35-
<string name="memory">Хранилище</string>
36-
<string name="memory_text">Разрешение для просмотра всех файлов на устройстве необходимо для правильного сохранения данных EXIF, пролистайте вниз и предоставьте полный доступ для продолжения работы</string>
37-
<string name="done">Готово</string>
3834
</resources>

app/src/main/res/values/strings.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,4 @@
3333
<string name="might_be_error_with_exif">If not JPEG compress format chosen, then EXIF metadata can be showed incorrectly in other apps</string>
3434
<string name="exif" translatable="false">EXIF</string>
3535
<string name="save">Save</string>
36-
<string name="grant">Grant</string>
37-
<string name="memory">Memory</string>
38-
<string name="memory_text">Access all files is needed to properly save EXIF, without this permission app wont work, scroll down and then found ImageResizer app</string>
39-
<string name="done">Done</string>
4036
</resources>

0 commit comments

Comments
 (0)