Skip to content
This repository was archived by the owner on Jul 25, 2024. It is now read-only.

Commit f88c746

Browse files
committed
Fix up crashes on file loads from Dropbox & Google Drive
1 parent 6fce5c2 commit f88c746

File tree

3 files changed

+79
-7
lines changed

3 files changed

+79
-7
lines changed

app/src/main/java/com/zulip/android/activities/ZulipActivity.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ public void onRequestPermissionsResult(int requestCode,
619619

620620
/**
621621
* Helper function to update UI to indicate image is being uploaded and call
622-
* {@link ZulipActivity#uploadFile(String)} to upload the image.
622+
* {@link ZulipActivity#uploadFile(File)} to upload the image.
623623
*/
624624
private void startFileUpload() {
625625
// Update UI to indicate image is being loaded
@@ -629,20 +629,33 @@ private void startFileUpload() {
629629
String loadingMsg = getResources().getString(R.string.uploading_message);
630630
sendingMessage(true, loadingMsg);
631631

632-
// get actual file path
633-
String imageFilePath = FilePathHelper.getPath(this, mImageUri);
632+
File file = null;
633+
if (FilePathHelper.isLegacy(mImageUri)) {
634+
file = FilePathHelper.getTempFileFromContentUri(this, mImageUri);
635+
} else {
636+
// get actual file path
637+
String imageFilePath = FilePathHelper.getPath(this, mImageUri);
638+
if (imageFilePath != null) {
639+
file = new File(imageFilePath);
640+
} else if ("content".equalsIgnoreCase(mImageUri.getScheme())) {
641+
file = FilePathHelper.getTempFileFromContentUri(this, mImageUri);
642+
}
643+
}
634644

645+
if (file == null) {
646+
Toast.makeText(this, R.string.invalid_image, Toast.LENGTH_SHORT).show();
647+
return;
648+
}
635649
// upload the file asynchronously to the server
636-
uploadFile(imageFilePath);
650+
uploadFile(file);
637651
}
638652

639653
/**
640654
* Function to upload file asynchronously to the server using retrofit callback
641655
* upload {@link com.zulip.android.service.ZulipServices#upload(MultipartBody.Part)}
642-
* @param filePath on local storage
656+
* @param file on local storage
643657
*/
644-
private void uploadFile(String filePath) {
645-
File file = new File(filePath);
658+
private void uploadFile(File file) {
646659

647660
// create RequestBody instance from file
648661
RequestBody requestFile =

app/src/main/java/com/zulip/android/util/FilePathHelper.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@
88
import android.os.Environment;
99
import android.provider.DocumentsContract;
1010
import android.provider.MediaStore;
11+
import android.widget.Toast;
12+
13+
import com.zulip.android.R;
14+
15+
import java.io.File;
16+
import java.io.FileOutputStream;
17+
import java.io.IOException;
18+
import java.io.InputStream;
1119

1220
/**
1321
* This class helps to find the actual file path from a given content Uri
@@ -63,6 +71,9 @@ else if (isMediaDocument(uri)) {
6371
return getDataColumn(context, contentUri, selection, selectionArgs);
6472
}
6573
}
74+
else if (isLegacy(uri)) {
75+
return null;
76+
}
6677
// MediaStore (and general)
6778
else if ("content".equalsIgnoreCase(uri.getScheme())) {
6879
return getDataColumn(context, uri, null, null);
@@ -101,6 +112,9 @@ private static String getDataColumn(Context context, Uri uri, String selection,
101112
final int column_index = cursor.getColumnIndexOrThrow(column);
102113
return cursor.getString(column_index);
103114
}
115+
} catch (IllegalArgumentException ex) {
116+
// there was no _data column for this content thing a ma bob
117+
return null;
104118
} finally {
105119
if (cursor != null)
106120
cursor.close();
@@ -132,4 +146,48 @@ private static boolean isDownloadsDocument(Uri uri) {
132146
private static boolean isMediaDocument(Uri uri) {
133147
return "com.android.providers.media.documents".equals(uri.getAuthority());
134148
}
149+
150+
/**
151+
* @param uri The Uri to check.
152+
* @return Whether the Uri authority is a legacy uri
153+
*/
154+
public static boolean isLegacy(Uri uri) {
155+
return "com.google.android.apps.docs.storage.legacy".equals(uri.getAuthority());
156+
}
157+
158+
public static File getTempFileFromContentUri(Context context, Uri uri) {
159+
InputStream is = null;
160+
File tempFile = null;
161+
try {
162+
is = context.getContentResolver().openInputStream(uri);
163+
tempFile = File.createTempFile("imgUpload", ".jpg");
164+
tempFile.deleteOnExit();
165+
FileOutputStream out = new FileOutputStream(tempFile);
166+
try {
167+
byte[] buffer = new byte[4 * 1024];
168+
int read;
169+
while ((read = is.read(buffer)) != -1) {
170+
out.write(buffer, 0, read);
171+
}
172+
out.flush();
173+
} finally {
174+
try {
175+
out.close();
176+
} catch (IOException e) {
177+
// ignore
178+
}
179+
}
180+
} catch (IOException e) {
181+
return null;
182+
} finally {
183+
if (is != null) {
184+
try {
185+
is.close();
186+
} catch (IOException e) {
187+
// ignored
188+
}
189+
}
190+
}
191+
return tempFile;
192+
}
135193
}

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,5 @@
9393
<string name="cannot_upload_image">Cannot Upload Image</string>
9494
<string name="cannot_find_image">Could not find the image</string>
9595
<string name="failed_to_upload">Failed to upload</string>
96+
<string name="invalid_image">Unable to load that image</string>
9697
</resources>

0 commit comments

Comments
 (0)