|
4 | 4 | import android.database.Cursor; |
5 | 5 | import android.net.Uri; |
6 | 6 | import android.provider.OpenableColumns; |
| 7 | +import java.io.File; |
| 8 | +import java.io.FileOutputStream; |
| 9 | +import java.io.InputStream; |
7 | 10 | import org.cagnulen.qdomyoszwift.QLog; |
8 | 11 |
|
9 | 12 | public class ContentHelper { |
10 | 13 |
|
11 | 14 | public static String getFileName(Context context, Uri uri) { |
| 15 | + if (uri == null) { |
| 16 | + return null; |
| 17 | + } |
| 18 | + |
12 | 19 | String result = null; |
13 | | - if (uri.getScheme().equals("content")) { |
| 20 | + String scheme = uri.getScheme(); |
| 21 | + if ("content".equals(scheme)) { |
14 | 22 | QLog.d("ContentHelper", "content"); |
15 | | - Cursor cursor = context.getContentResolver().query(uri, null, null, null, null); |
16 | | - QLog.d("ContentHelper", "cursor " + cursor); |
| 23 | + Cursor cursor = null; |
17 | 24 | try { |
| 25 | + cursor = context.getContentResolver().query(uri, new String[] {OpenableColumns.DISPLAY_NAME}, null, null, null); |
| 26 | + QLog.d("ContentHelper", "cursor " + cursor); |
18 | 27 | if (cursor != null && cursor.moveToFirst()) { |
19 | | - result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME)); |
20 | | - QLog.d("ContentHelper", "result " + result); |
| 28 | + int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); |
| 29 | + if (nameIndex >= 0) { |
| 30 | + result = cursor.getString(nameIndex); |
| 31 | + QLog.d("ContentHelper", "result " + result); |
| 32 | + } |
21 | 33 | } |
| 34 | + } catch (Exception e) { |
| 35 | + QLog.d("ContentHelper", "getFileName query failed " + e); |
22 | 36 | } finally { |
23 | | - cursor.close(); |
| 37 | + if (cursor != null) { |
| 38 | + cursor.close(); |
| 39 | + } |
24 | 40 | } |
25 | 41 | } |
| 42 | + if ((result == null || result.isEmpty()) && uri.getLastPathSegment() != null) { |
| 43 | + result = uri.getLastPathSegment(); |
| 44 | + } |
26 | 45 | return result; |
27 | 46 | } |
| 47 | + |
| 48 | + public static boolean copyContentToFile(Context context, Uri uri, String destinationPath) { |
| 49 | + if (context == null || uri == null || destinationPath == null || destinationPath.isEmpty()) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + InputStream inputStream = null; |
| 54 | + FileOutputStream outputStream = null; |
| 55 | + try { |
| 56 | + inputStream = context.getContentResolver().openInputStream(uri); |
| 57 | + if (inputStream == null) { |
| 58 | + QLog.d("ContentHelper", "copyContentToFile null input stream for " + uri); |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + outputStream = new FileOutputStream(destinationPath, false); |
| 63 | + byte[] buffer = new byte[8192]; |
| 64 | + int read; |
| 65 | + while ((read = inputStream.read(buffer)) != -1) { |
| 66 | + outputStream.write(buffer, 0, read); |
| 67 | + } |
| 68 | + outputStream.flush(); |
| 69 | + return true; |
| 70 | + } catch (Exception e) { |
| 71 | + QLog.d("ContentHelper", "copyContentToFile failed " + e); |
| 72 | + return false; |
| 73 | + } finally { |
| 74 | + try { |
| 75 | + if (inputStream != null) { |
| 76 | + inputStream.close(); |
| 77 | + } |
| 78 | + } catch (Exception ignored) { |
| 79 | + } |
| 80 | + try { |
| 81 | + if (outputStream != null) { |
| 82 | + outputStream.close(); |
| 83 | + } |
| 84 | + } catch (Exception ignored) { |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public static String importContentToAppDir(Context context, Uri uri, String destinationDirPath) { |
| 90 | + if (context == null || uri == null || destinationDirPath == null || destinationDirPath.isEmpty()) { |
| 91 | + return ""; |
| 92 | + } |
| 93 | + |
| 94 | + String fileName = sanitizeFileName(getFileName(context, uri)); |
| 95 | + if (fileName.isEmpty()) { |
| 96 | + fileName = "imported_file"; |
| 97 | + } |
| 98 | + |
| 99 | + File destinationDir = new File(destinationDirPath); |
| 100 | + if (!destinationDir.exists() && !destinationDir.mkdirs()) { |
| 101 | + QLog.d("ContentHelper", "importContentToAppDir could not create " + destinationDirPath); |
| 102 | + return ""; |
| 103 | + } |
| 104 | + |
| 105 | + File destinationFile = new File(destinationDir, fileName); |
| 106 | + if (destinationFile.exists() && !destinationFile.delete()) { |
| 107 | + QLog.d("ContentHelper", "importContentToAppDir could not replace " + destinationFile.getAbsolutePath()); |
| 108 | + return ""; |
| 109 | + } |
| 110 | + |
| 111 | + if (!copyContentToFile(context, uri, destinationFile.getAbsolutePath())) { |
| 112 | + if (destinationFile.exists()) { |
| 113 | + destinationFile.delete(); |
| 114 | + } |
| 115 | + return ""; |
| 116 | + } |
| 117 | + |
| 118 | + return destinationFile.getAbsolutePath(); |
| 119 | + } |
| 120 | + |
| 121 | + private static String sanitizeFileName(String value) { |
| 122 | + if (value == null) { |
| 123 | + return ""; |
| 124 | + } |
| 125 | + |
| 126 | + return value.replace('\\', '_').replace('/', '_').trim(); |
| 127 | + } |
28 | 128 | } |
0 commit comments