|
2 | 2 |
|
3 | 3 | import android.content.ContentResolver;
|
4 | 4 | import android.content.Intent;
|
| 5 | +import android.database.Cursor; |
5 | 6 | import android.net.Uri;
|
6 | 7 | import android.os.Bundle;
|
7 | 8 | import android.os.Handler;
|
| 9 | +import android.provider.MediaStore; |
| 10 | +import android.provider.OpenableColumns; |
8 | 11 | import android.util.Log;
|
9 | 12 | import android.widget.ProgressBar;
|
10 | 13 | import android.widget.TextView;
|
11 | 14 | import androidx.annotation.Nullable;
|
12 | 15 | import androidx.appcompat.app.AppCompatActivity;
|
| 16 | + |
13 | 17 | import java.util.Collections;
|
14 | 18 | import java.util.List;
|
15 | 19 | import org.docspell.docspellshare.R;
|
@@ -128,8 +132,53 @@ void handleFiles(List<Uri> uris, String url) {
|
128 | 132 | HttpRequest.Builder req = HttpRequest.newBuilder().setUrl(url);
|
129 | 133 | ContentResolver resolver = getContentResolver();
|
130 | 134 | for (Uri uri : uris) {
|
131 |
| - req.addFile(resolver, uri); |
| 135 | + req.addFile(resolver, uri, parseFilenameFromUri(uri)); |
132 | 136 | }
|
133 | 137 | UploadManager.getInstance().submit(req.build());
|
134 | 138 | }
|
| 139 | + |
| 140 | + /** |
| 141 | + * Parses the filename for the passed uri. |
| 142 | + * Depending on the source application, a file:// or content:// will be shared. |
| 143 | + * If a file:// is shared, the filename/title is the last segment of the uri. |
| 144 | + * IF NOT, the last segment of the uri is the android document id which is not related to the |
| 145 | + * document at all. |
| 146 | + * Therefore, a cursor has to be defined to read the content's metadata to obtain the name/title |
| 147 | + * |
| 148 | + * See https://developer.android.com/training/secure-file-sharing/retrieve-info?hl=en for ref |
| 149 | + * |
| 150 | + * @param uri link to content/file the name should be parsed from |
| 151 | + * @return parsed "real" name of the content/file |
| 152 | + */ |
| 153 | + private String parseFilenameFromUri(Uri uri) { |
| 154 | + String fileName = null; |
| 155 | + if (uri.getScheme().equals("file")) { |
| 156 | + fileName = uri.getLastPathSegment(); |
| 157 | + } else { |
| 158 | + Cursor cursor = null; |
| 159 | + try { |
| 160 | + cursor = getContentResolver().query( |
| 161 | + uri, |
| 162 | + new String[]{ |
| 163 | + MediaStore.Images.ImageColumns.DISPLAY_NAME |
| 164 | + }, |
| 165 | + null, |
| 166 | + null, |
| 167 | + null |
| 168 | + ); |
| 169 | + if (cursor != null && cursor.moveToFirst()) { |
| 170 | + fileName = cursor.getString(cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME)); |
| 171 | + } |
| 172 | + } catch (IllegalArgumentException e) { |
| 173 | + // exception will be thrown if index is out of range (e.x. -1). |
| 174 | + // No need to handle the exception since the HttpRequest Class will fallback to Document ID |
| 175 | + } finally { |
| 176 | + if (cursor != null) { |
| 177 | + cursor.close(); |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + return fileName; |
| 182 | + } |
135 | 183 | }
|
| 184 | + |
0 commit comments