From 8d62114762092e31f3465e17d764d92c94ac4fa5 Mon Sep 17 00:00:00 2001 From: Matt Holgate Date: Wed, 2 Jul 2014 16:42:25 +0100 Subject: Fix opening files from Email app. On 2.3.x, opening files from the Android email app (not Gmail), was causing a crash due to a SecurityException. On 4.x, it was failing with an error message. I think we should have just been calling getContentResolver().openInputStream(uri) to get the file data, rather than reading the row from the content resolver and looking at the _data field. On 2.3.x querying this row caused a security error. On 4.x, we got back a _data field containing a path, but this path was internal to the email app, and not for our consumption. Previously we were calling openInputStream() only as a fallback; now we try it first, and fall back to the other method if that fails. I suspect we can delete the other code, but I can't test on the 3.x version of the Transformer Prime, so I'm leaving it be for now. --- .../src/com/artifex/mupdfdemo/MuPDFActivity.java | 87 ++++++++++++---------- 1 file changed, 48 insertions(+), 39 deletions(-) (limited to 'platform/android/src/com') diff --git a/platform/android/src/com/artifex/mupdfdemo/MuPDFActivity.java b/platform/android/src/com/artifex/mupdfdemo/MuPDFActivity.java index 6bb45a93..bf652d92 100644 --- a/platform/android/src/com/artifex/mupdfdemo/MuPDFActivity.java +++ b/platform/android/src/com/artifex/mupdfdemo/MuPDFActivity.java @@ -261,47 +261,56 @@ public class MuPDFActivity extends Activity implements FilePicker.FilePickerSupp if (Intent.ACTION_VIEW.equals(intent.getAction())) { Uri uri = intent.getData(); if (uri.toString().startsWith("content://")) { - // Handle view requests from the Transformer Prime's file manager - // Hopefully other file managers will use this same scheme, if not - // using explicit paths. - Cursor cursor = getContentResolver().query(uri, new String[]{"_data"}, null, null, null); - if (cursor.moveToFirst()) { - String str = cursor.getString(0); - String reason = null; - if (str == null) { - try { - InputStream is = getContentResolver().openInputStream(uri); - int len = is.available(); - buffer = new byte[len]; - is.read(buffer, 0, len); - is.close(); - } - catch (java.lang.OutOfMemoryError e) - { - System.out.println("Out of memory during buffer reading"); - reason = e.toString(); - } - catch (Exception e) { - reason = e.toString(); - } - if (reason != null) - { - buffer = null; - Resources res = getResources(); - AlertDialog alert = mAlertBuilder.create(); - setTitle(String.format(res.getString(R.string.cannot_open_document_Reason), reason)); - alert.setButton(AlertDialog.BUTTON_POSITIVE, getString(R.string.dismiss), - new DialogInterface.OnClickListener() { - public void onClick(DialogInterface dialog, int which) { - finish(); - } - }); - alert.show(); - return; + String reason = null; + try { + InputStream is = getContentResolver().openInputStream(uri); + int len = is.available(); + buffer = new byte[len]; + is.read(buffer, 0, len); + is.close(); + } + catch (java.lang.OutOfMemoryError e) { + System.out.println("Out of memory during buffer reading"); + reason = e.toString(); + } + catch (Exception e) { + System.out.println("Exception reading from stream: " + e); + + // Handle view requests from the Transformer Prime's file manager + // Hopefully other file managers will use this same scheme, if not + // using explicit paths. + // I'm hoping that this case below is no longer needed...but it's + // hard to test as the file manager seems to have changed in 4.x. + try { + Cursor cursor = getContentResolver().query(uri, new String[]{"_data"}, null, null, null); + if (cursor.moveToFirst()) { + String str = cursor.getString(0); + if (str == null) { + reason = "Couldn't parse data in intent"; + } + else { + uri = Uri.parse(str); + } } - } else { - uri = Uri.parse(str); } + catch (Exception e2) { + System.out.println("Exception in Transformer Prime file manager code: " + e2); + reason = e2.toString(); + } + } + if (reason != null) { + buffer = null; + Resources res = getResources(); + AlertDialog alert = mAlertBuilder.create(); + setTitle(String.format(res.getString(R.string.cannot_open_document_Reason), reason)); + alert.setButton(AlertDialog.BUTTON_POSITIVE, getString(R.string.dismiss), + new DialogInterface.OnClickListener() { + public void onClick(DialogInterface dialog, int which) { + finish(); + } + }); + alert.show(); + return; } } if (buffer != null) { -- cgit v1.2.3