Skip to content

Commit 54a0429

Browse files
freedomofkeimagsantner
authored andcommitted
Fix problem with cloud storage picture sources, fixes #38 (#37)
1 parent 005aa84 commit 54a0429

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

app/src/main/java/io/github/gsantner/memetastic/activity/MainActivity.java

+30-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import android.os.Bundle;
1313
import android.os.Environment;
1414
import android.os.Handler;
15+
import android.os.ParcelFileDescriptor;
1516
import android.provider.MediaStore;
1617
import android.support.annotation.DrawableRes;
1718
import android.support.annotation.NonNull;
@@ -40,9 +41,12 @@
4041
import android.widget.ProgressBar;
4142
import android.widget.TextView;
4243

44+
import net.gsantner.opoc.util.FileUtils;
4345
import net.gsantner.opoc.util.SimpleMarkdownParser;
4446

4547
import java.io.File;
48+
import java.io.FileDescriptor;
49+
import java.io.FileInputStream;
4650
import java.io.IOException;
4751
import java.lang.reflect.Method;
4852
import java.util.ArrayList;
@@ -383,8 +387,32 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
383387
String picturePath = cursor.getString(columnIndex);
384388
cursor.close();
385389

386-
// String picturePath contains the path of selected Image
387-
onImageTemplateWasChosen(picturePath);
390+
if (picturePath == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
391+
// Retrieve image from Cloud, e.g.: Google Drive, Picasa
392+
try {
393+
ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(selectedImage, "r");
394+
if (parcelFileDescriptor != null) {
395+
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
396+
FileInputStream input = new FileInputStream(fileDescriptor);
397+
398+
// Create temporary file in cache directory
399+
picturePath = File.createTempFile("image", "tmp", getCacheDir()).getAbsolutePath();
400+
FileUtils.writeFile(
401+
new File(picturePath),
402+
FileUtils.readCloseBinaryStream(input)
403+
);
404+
}
405+
} catch (IOException e) {
406+
// nothing we can do here, null value will be handled below
407+
}
408+
}
409+
410+
if (picturePath == null) { // All checks fail
411+
ActivityUtils.get(this).showSnackBar(R.string.main__error_fail_retrieve_picture, false);
412+
} else {
413+
// String picturePath contains the path of selected Image
414+
onImageTemplateWasChosen(picturePath);
415+
}
388416
}
389417
} else {
390418
ActivityUtils.get(this).showSnackBar(R.string.main__error_no_picture_selected, false);

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

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
<string name="main__get_picture_from_camera">Get picture from Camera</string>
2323
<string name="main__error_camera_cannot_start">Could not start Camera.</string>
2424
<string name="main__error_no_picture_selected">No picture was selected.</string>
25+
<string name="main__error_fail_retrieve_picture">Could not load picture from storage.</string>
2526
<string name="main__share_meme_prompt">Share your Meme via…</string>
2627

2728
<string name="main__donate">Donate</string>

0 commit comments

Comments
 (0)