Get File object for the given Android URI. Use content resolver to get real path if direct path doesn't return valid file.
(Context context, Uri uri)
| 502 | * Use content resolver to get real path if direct path doesn't return valid file. |
| 503 | */ |
| 504 | private static File getFileFromUri(Context context, Uri uri) |
| 505 | { |
| 506 | |
| 507 | // first try by direct path |
| 508 | File file = new File(uri.getPath()); |
| 509 | if(file.exists()) |
| 510 | { |
| 511 | return file; |
| 512 | } |
| 513 | |
| 514 | // try reading real path from content resolver (gallery images) |
| 515 | Cursor cursor = null; |
| 516 | try |
| 517 | { |
| 518 | String[] proj = |
| 519 | { MediaStore.Images.Media.DATA }; |
| 520 | cursor = context.getContentResolver().query(uri, proj, null, null, null); |
| 521 | int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); |
| 522 | cursor.moveToFirst(); |
| 523 | String realPath = cursor.getString(column_index); |
| 524 | file = new File(realPath); |
| 525 | } |
| 526 | catch(Exception ignored) |
| 527 | { |
| 528 | } |
| 529 | finally |
| 530 | { |
| 531 | if(cursor != null) |
| 532 | { |
| 533 | cursor.close(); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | return file; |
| 538 | } |
| 539 | |
| 540 | /** |
| 541 | * Rotate the given bitmap by the given degrees.<br> |