blob: 9772b03fac463227e76e087106c534c42142384a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
package de.thedevstack.conversationsplus.utils;
import android.annotation.TargetApi;
import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import de.thedevstack.conversationsplus.ConversationsPlusApplication;
/**
* Created by tzur on 30.10.2015.
*/
public final class FileHelper {
/**
* taken from: http://stackoverflow.com/a/29164361
* @param uri
* @return
*/
@TargetApi(19)
private static String getRealPathFromUriLollipop(Uri uri) {
String path = null;
String wholeID = DocumentsContract.getDocumentId(uri);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = ConversationsPlusApplication.getInstance().getContentResolver().
query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{ id }, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
path = cursor.getString(columnIndex);
}
cursor.close();
return path;
}
/**
* Get the real path from an Uri.
* @param uri the uri to convert to the real path
* @return the real path or <code>null</code>
*/
public static String getRealPathFromUri(Uri uri) {
String path = null;
if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
return uri.getPath();
} else if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
String[] projection = {MediaStore.MediaColumns.DATA};
Cursor metaCursor = ConversationsPlusApplication.getInstance().getContentResolver().query(uri,
projection, null, null, null);
if (metaCursor != null) {
try {
if (metaCursor.moveToFirst()) {
path = metaCursor.getString(0);
}
} finally {
metaCursor.close();
}
}
}
if (path == null) {
path = getRealPathFromUriLollipop(uri);
}
return path;
}
private FileHelper() {
// Utility class - do not instantiate
}
}
|