Skip to content

Commit

Permalink
Fix problem with cloud storage picture sources, fixes gsantner#38 (gs…
Browse files Browse the repository at this point in the history
  • Loading branch information
freedomofkeima authored and gsantner committed Oct 1, 2017
1 parent 005aa84 commit 54a0429
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.support.annotation.DrawableRes;
import android.support.annotation.NonNull;
Expand Down Expand Up @@ -40,9 +41,12 @@
import android.widget.ProgressBar;
import android.widget.TextView;

import net.gsantner.opoc.util.FileUtils;
import net.gsantner.opoc.util.SimpleMarkdownParser;

import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.ArrayList;
Expand Down Expand Up @@ -383,8 +387,32 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String picturePath = cursor.getString(columnIndex);
cursor.close();

// String picturePath contains the path of selected Image
onImageTemplateWasChosen(picturePath);
if (picturePath == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
// Retrieve image from Cloud, e.g.: Google Drive, Picasa
try {
ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(selectedImage, "r");
if (parcelFileDescriptor != null) {
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
FileInputStream input = new FileInputStream(fileDescriptor);

// Create temporary file in cache directory
picturePath = File.createTempFile("image", "tmp", getCacheDir()).getAbsolutePath();
FileUtils.writeFile(
new File(picturePath),
FileUtils.readCloseBinaryStream(input)
);
}
} catch (IOException e) {
// nothing we can do here, null value will be handled below
}
}

if (picturePath == null) { // All checks fail
ActivityUtils.get(this).showSnackBar(R.string.main__error_fail_retrieve_picture, false);
} else {
// String picturePath contains the path of selected Image
onImageTemplateWasChosen(picturePath);
}
}
} else {
ActivityUtils.get(this).showSnackBar(R.string.main__error_no_picture_selected, false);
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<string name="main__get_picture_from_camera">Get picture from Camera</string>
<string name="main__error_camera_cannot_start">Could not start Camera.</string>
<string name="main__error_no_picture_selected">No picture was selected.</string>
<string name="main__error_fail_retrieve_picture">Could not load picture from storage.</string>
<string name="main__share_meme_prompt">Share your Meme via…</string>

<string name="main__donate">Donate</string>
Expand Down

0 comments on commit 54a0429

Please sign in to comment.