|
12 | 12 | import android.os.Bundle;
|
13 | 13 | import android.os.Environment;
|
14 | 14 | import android.os.Handler;
|
| 15 | +import android.os.ParcelFileDescriptor; |
15 | 16 | import android.provider.MediaStore;
|
16 | 17 | import android.support.annotation.DrawableRes;
|
17 | 18 | import android.support.annotation.NonNull;
|
|
40 | 41 | import android.widget.ProgressBar;
|
41 | 42 | import android.widget.TextView;
|
42 | 43 |
|
| 44 | +import net.gsantner.opoc.util.FileUtils; |
43 | 45 | import net.gsantner.opoc.util.SimpleMarkdownParser;
|
44 | 46 |
|
45 | 47 | import java.io.File;
|
| 48 | +import java.io.FileDescriptor; |
| 49 | +import java.io.FileInputStream; |
46 | 50 | import java.io.IOException;
|
47 | 51 | import java.lang.reflect.Method;
|
48 | 52 | import java.util.ArrayList;
|
@@ -383,8 +387,32 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
|
383 | 387 | String picturePath = cursor.getString(columnIndex);
|
384 | 388 | cursor.close();
|
385 | 389 |
|
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 | + } |
388 | 416 | }
|
389 | 417 | } else {
|
390 | 418 | ActivityUtils.get(this).showSnackBar(R.string.main__error_no_picture_selected, false);
|
|
0 commit comments