fix potential crash when trying to add attachment. Action GetContent is no longer recommended (even though OpenDocument does not show (most) third party apps).

This commit is contained in:
Philipp Crocoll
2017-05-08 13:33:10 +02:00
parent 29b9478a59
commit dbcb9ed55a
2 changed files with 12 additions and 3 deletions

View File

@@ -33,6 +33,7 @@ using KeePassLib.Security;
using Android.Content.PM; using Android.Content.PM;
using System.IO; using System.IO;
using System.Globalization; using System.Globalization;
using Android.Database;
using Android.Graphics; using Android.Graphics;
using Android.Util; using Android.Util;
using Debug = System.Diagnostics.Debug; using Debug = System.Diagnostics.Debug;
@@ -523,14 +524,21 @@ namespace keepass2android
String result = null; String result = null;
if (uri.Scheme.Equals("content")) if (uri.Scheme.Equals("content"))
{ {
var cursor = ContentResolver.Query(uri, null, null, null, null); ICursor cursor = null;
try try
{ {
cursor = ContentResolver.Query(uri, null, null, null, null);
if (cursor != null && cursor.MoveToFirst()) if (cursor != null && cursor.MoveToFirst())
{ {
result = cursor.GetString(cursor.GetColumnIndex(OpenableColumns.DisplayName)); result = cursor.GetString(cursor.GetColumnIndex(OpenableColumns.DisplayName));
} }
} }
catch (Exception e)
{
Kp2aLog.Log(e.ToString());
}
finally finally
{ {
if (cursor != null) if (cursor != null)
@@ -621,8 +629,9 @@ namespace keepass2android
//Android standard way to read the contents (content or file scheme) //Android standard way to read the contents (content or file scheme)
vBytes = ReadFully(ContentResolver.OpenInputStream(filename)); vBytes = ReadFully(ContentResolver.OpenInputStream(filename));
} }
catch (Exception) catch (Exception ex)
{ {
Kp2aLog.Log(ex.ToString());
//if standard way fails, try to read as a file //if standard way fails, try to read as a file
vBytes = File.ReadAllBytes(filename.Path); vBytes = File.ReadAllBytes(filename.Path);
} }

View File

@@ -200,7 +200,7 @@ namespace keepass2android
/// is more for one-time access, but therefore allows possibly more available sources.</param> /// is more for one-time access, but therefore allows possibly more available sources.</param>
public static void ShowBrowseDialog(Activity activity, int requestCodeBrowse, bool forSaving, bool tryGetPermanentAccess) public static void ShowBrowseDialog(Activity activity, int requestCodeBrowse, bool forSaving, bool tryGetPermanentAccess)
{ {
var loadAction = (tryGetPermanentAccess && IsKitKatOrLater) ? var loadAction = IsKitKatOrLater ?
Intent.ActionOpenDocument : Intent.ActionGetContent; Intent.ActionOpenDocument : Intent.ActionGetContent;
if ((!forSaving) && (IsIntentAvailable(activity, loadAction, "*/*", new List<string> { Intent.CategoryOpenable}))) if ((!forSaving) && (IsIntentAvailable(activity, loadAction, "*/*", new List<string> { Intent.CategoryOpenable})))
{ {