get real file name for files shared from Conversations

This commit is contained in:
Daniel Gultsch 2018-04-15 17:10:50 +02:00
parent 93f405d9a1
commit be5e39a440
2 changed files with 19 additions and 9 deletions

View file

@ -536,8 +536,7 @@ public class FileBackend {
public static Uri getUriForFile(Context context, File file) { public static Uri getUriForFile(Context context, File file) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N || Config.ONLY_INTERNAL_STORAGE) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N || Config.ONLY_INTERNAL_STORAGE) {
try { try {
String packageId = context.getPackageName(); return FileProvider.getUriForFile(context, getAuthority(context), file);
return FileProvider.getUriForFile(context, packageId + FILE_PROVIDER, file);
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
throw new SecurityException(e); throw new SecurityException(e);
@ -550,6 +549,10 @@ public class FileBackend {
} }
} }
public static String getAuthority(Context context) {
return context.getPackageName() + FILE_PROVIDER;
}
public static Uri getIndexableTakePhotoUri(Uri original) { public static Uri getIndexableTakePhotoUri(Uri original) {
if (Config.ONLY_INTERNAL_STORAGE || "file".equals(original.getScheme())) { if (Config.ONLY_INTERNAL_STORAGE || "file".equals(original.getScheme())) {
return original; return original;
@ -727,9 +730,7 @@ public class FileBackend {
input = rotate(input, getRotation(image)); input = rotate(input, getRotation(image));
return cropCenterSquare(input, size); return cropCenterSquare(input, size);
} }
} catch (SecurityException e) { } catch (FileNotFoundException | SecurityException e) {
return null; // happens for example on Android 6.0 if contacts permissions get revoked
} catch (FileNotFoundException e) {
return null; return null;
} finally { } finally {
close(is); close(is);

View file

@ -11,6 +11,9 @@ import android.provider.DocumentsContract;
import android.provider.MediaStore; import android.provider.MediaStore;
import java.io.File; import java.io.File;
import java.util.List;
import eu.siacs.conversations.persistance.FileBackend;
public class FileUtils { public class FileUtils {
@ -83,7 +86,13 @@ public class FileUtils {
} }
// MediaStore (and general) // MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) { else if ("content".equalsIgnoreCase(uri.getScheme())) {
String path = getDataColumn(context, uri, null, null); List<String> segments = uri.getPathSegments();
String path;
if (FileBackend.getAuthority(context).equals(uri.getAuthority()) && segments.size() > 1 && segments.get(0).equals("external")) {
path = Environment.getExternalStorageDirectory().getAbsolutePath() + uri.getPath().substring(segments.get(0).length() + 1);
} else {
path = getDataColumn(context, uri, null, null);
}
if (path != null) { if (path != null) {
File file = new File(path); File file = new File(path);
if (!file.canRead()) { if (!file.canRead()) {