Code: Select all
Intent.ACTION_EDIT< /code>) Die Mediendatei der App aus dem privaten Ordner der App in Google Photos,
, aber es scheint, dass Google -Fotos nur Medien -URI unterstützt, und wir können keinen für den privaten Speicher der App erhalten. Die Datei muss öffentlich verfügbar sein. < /p>
private const val GOOGLE_PHOTOS = "com.google.android.apps.photos"
fun editVideoFile(context: Context, inputUri: Uri, mimeType: String) {
val uri = getUriForAppDirIfNeeded(context, inputUri)
// ways to get media uri
// but it won't work for app's private files at
// `data/user/0/com.test.example/files/*`
// MediaStore.getMediaUri(context, uri)
// MediaScannerConnection.scanFile(
// context,
// arrayOf(file.absolutePath),
// null
// ) { path, uri ->
// val mediaUri = uri.toString().replace("images/media", "file")
// .replace("video/media", "file").toUri()
// }
try {
val intent = Intent(Intent.ACTION_EDIT).apply {
setPackage(GOOGLE_PHOTOS)
setDataAndType(
/* data = */ uri, // HAS TO BE MEDIA URI (a public file), other won't work...
/* type = */ mimeType
)
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
}
context.startActivity(intent)
} catch (e: Throwable) {
openAppInGooglePlay(context, GOOGLE_PHOTOS)
}
}
private fun getUriForAppDirIfNeeded(
context: Context,
uri: Uri,
): Uri {
try {
val file = uri.toFile()
@Suppress("DEPRECATION")
if ((context.externalMediaDirs.filterNotNull() + context.filesDir).any { file.startsWith(it) }) {
return FileProvider.getUriForFile(
context,
"${context.packageName}.provider",
file
)
}
} catch (e: Throwable) {
// ignore
}
return uri
}
< /code>
Aber öffnen (Intent.ACTION_VIEW
Code: Select all
fun openFileInVideoPlayer(context: Context, uri: Uri, mimeType: String) {
try {
val intent = Intent(Intent.ACTION_VIEW).apply {
setPackage(GOOGLE_PHOTOS)
setDataAndType(
/* data = */ getUriForAppDirIfNeeded(context, uri),
/* type = */ mimeType
)
flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
}
context.startActivity(intent)
} catch (e: Throwable) {
openAppInGooglePlay(context, GOOGLE_PHOTOS)
}
}
< /code>
Beispiel für die private URI der App: < /p>
Die private Datei der App:
file:///data/user/0/com.test.example/files/records/2025-04-30%2023h%2023m%2004s.mp4
URI für externe Apps über FileProvider (nach dem Aufrufen von GeturiForAppdirifNeed Funktion):
Code: Select all
content://com.test.example.provider/records/2025-04-30%2023h%2023m%2004s.mp4
Aber Googlefotos bearbeiten nur mit Medien -URI:
Code: Select all
content://media/external_primary/file/1000005008