|
29 | 29 | import java.io.FileInputStream;
|
30 | 30 | import java.io.ByteArrayOutputStream;
|
31 | 31 | import java.io.FileNotFoundException;
|
| 32 | +import java.io.OutputStream; |
32 | 33 | import java.util.List;
|
33 | 34 | import java.util.ArrayList;
|
34 | 35 | import java.io.FileOutputStream;
|
@@ -220,6 +221,18 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
|
220 | 221 | : data.getData();
|
221 | 222 |
|
222 | 223 | String realPath = getRealPathFromURI(uri);
|
| 224 | + if (realPath == null) { |
| 225 | + try { |
| 226 | + File file = createFileFromURI(uri); |
| 227 | + realPath = file.getAbsolutePath(); |
| 228 | + uri = Uri.fromFile(file); |
| 229 | + } |
| 230 | + catch(Exception e) { |
| 231 | + response.putString("error", "Could not read photo"); |
| 232 | + mCallback.invoke(response); |
| 233 | + return; |
| 234 | + } |
| 235 | + } |
223 | 236 |
|
224 | 237 | boolean isUrl = true;
|
225 | 238 | try {
|
@@ -300,6 +313,26 @@ private String getRealPathFromURI(Uri uri) {
|
300 | 313 | return result;
|
301 | 314 | }
|
302 | 315 |
|
| 316 | + private File createFileFromURI(Uri uri) throws Exception { |
| 317 | + File file = new File(mReactContext.getCacheDir(), "photo-" + uri.getLastPathSegment()); |
| 318 | + InputStream input = mReactContext.getContentResolver().openInputStream(uri); |
| 319 | + OutputStream output = new FileOutputStream(file); |
| 320 | + |
| 321 | + try { |
| 322 | + byte[] buffer = new byte[4 * 1024]; |
| 323 | + int read; |
| 324 | + while ((read = input.read(buffer)) != -1) { |
| 325 | + output.write(buffer, 0, read); |
| 326 | + } |
| 327 | + output.flush(); |
| 328 | + } finally { |
| 329 | + output.close(); |
| 330 | + input.close(); |
| 331 | + } |
| 332 | + |
| 333 | + return file; |
| 334 | + } |
| 335 | + |
303 | 336 | private String getBase64StringFromFile (String absoluteFilePath) {
|
304 | 337 | InputStream inputStream = null;
|
305 | 338 | try {
|
|
0 commit comments