1

I'm developing an app that allows users to take photos, draw on them, and then upload them into our social media application. I would like to use a third-party app for the drawing to lessen the work load on us. I found a number of answers on here about how to do so, and much of the following code is taken from those answers. When I try different combinations of answers, I always get Toast errors on the Intent. The two errors I get are "File could not be opened" and "Image could not be edited". The former sounds like file permissions problems, which I think should be getting resolved from the grantUriPermissions work-around below. The other error I have no idea about.

// Code taken from answer on http://stackoverflow.com/questions/15699299/android-edit-image-intent
final Uri uri = Uri.parse(this.photoPath);
int flags = Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION;

Intent editIntent = new Intent(Intent.ACTION_EDIT);
editIntent.setDataAndType(uri, "image/*");
editIntent.addFlags(flags);
editIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);

// This work-around allows the intent to access our private FileProvider storage.
// Code taken from http://stackoverflow.com/questions/24835364/android-open-private-file-with-third-party-app
List<ResolveInfo> resInfoList = this.getPackageManager().queryIntentActivities(editIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
    String packageName = resolveInfo.activityInfo.packageName;
    this.grantUriPermission(packageName, uri, flags);
}

startActivityForResult(Intent.createChooser(editIntent, null), EDIT_INTENT);

Anybody seen these Toast messages before and been able to resolve them?

BowlingHawk95
  • 1,518
  • 10
  • 15
  • 1
    Where is `photoPath` pointing? Also note that those `Intent` flags and that `grantUriPermission()` loop are useless for files; those are for `Uri` values from a `ContentProvider`, such as `FileProvider`. Also, that `putExtra()` call is useless, as `EXTRA_OUTPUT` is used for `ACTION_IMAGE_CAPTURE` and `ACTION_VIDEO_CAPTURE`, [not `ACTION_EDIT`](https://developer.android.com/reference/android/content/Intent.html#ACTION_EDIT). – CommonsWare Apr 08 '17 at 23:22
  • photoPath is the result of calling .getAbsolutePath() on a File created earlier. And the file is being managed by a FileProvider in the application, which I why I feel that I need the loop. I could be mistaken on that; I don't know all the details of Android file permissions. – BowlingHawk95 Apr 09 '17 at 01:30

1 Answers1

0

I figured out the problem. My use of Uri.parse was wrong because I'm using a FileProvider in my application. Exchanging that line for FileProvider.getUriForFile made the app work as intended.

BowlingHawk95
  • 1,518
  • 10
  • 15
  • I have tried your code, but, I can't find a way to return the edited image file path back. I have checked `onActivityResult()`, but, the `data` parameter always returns `null` with result `Activity.RESULT_CANCELED` and I cannot find a way to make it `Activity.RESULT_OK`. Therefore, how can you manage to return the image file path to `onActivityResult()` ? – Yusril Maulidan Raji Sep 23 '19 at 10:14