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

View file

@ -11,6 +11,9 @@ import android.provider.DocumentsContract;
import android.provider.MediaStore;
import java.io.File;
import java.util.List;
import eu.siacs.conversations.persistance.FileBackend;
public class FileUtils {
@ -83,7 +86,13 @@ public class FileUtils {
}
// MediaStore (and general)
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) {
File file = new File(path);
if (!file.canRead()) {
@ -111,7 +120,7 @@ public class FileUtils {
* @return The value of the _data column, which is typically a file path.
*/
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
@ -120,12 +129,12 @@ public class FileUtils {
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,null);
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} catch(Exception e) {
} catch (Exception e) {
return null;
} finally {
if (cursor != null) {