0

I'm having an odd problem trying to share an image to another application. Sharing works on a Nexus 5, but fails on a Nexus 6 - both are running 6.0.1, both are compiled exactly the same.

What happens is that sharing to Keep (or any other application, for that matter) works just fine on the 5. However, on the Nexus 6, Keep fails to attach the image. logcat shows the following:

08-19 10:25:15.232  4491  4491 E Keep    : Image file not found
08-19 10:25:15.232  4491  4491 E Keep    : java.io.FileNotFoundException: /storage/emulated/0/Android/data/org.foo.bar/cache/share-1471627513628.png: open failed: EACCES (Permission denied)

File is written successfully, so it's not a permission issue. I can see the file (using adb shell and ls) at the location, so it's not that it's inaccessible. I'm even able to download the file using adb pull and open it in an image viewer, so it's not corrupted.

Neither device is rooted, and this feature was working previously on the Nexus 6 without a problem.

Here's the relevant code:

File outputFile = new File(context.getExternalCacheDir(),
        "share-" + System.currentTimeMillis() + ".png");

Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);

// ..Do the actual rendering..

OutputStream stream = new FileOutputStream(outputFile.toString());
bmp.compress(Bitmap.CompressFormat.PNG, 80, stream);
stream.close();

Uri fileUri = Uri.fromFile(outputFile);

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("*/*");
intent.putExtra(Intent.EXTRA_TEXT, message);
intent.putExtra(Intent.EXTRA_STREAM, fileUri);

context.startActivity(Intent.createChooser(intent,
        context.getString(R.string.share)));

This post suggests that this is a bug in Android, but I'm not so sure.

Some relevant information from build.gradle:

compileSdkVersion 23
buildToolsVersion "23.0.3"

defaultConfig {
    minSdkVersion 15
    targetSdkVersion 23
}

Any suggestions?

Community
  • 1
  • 1
Melllvar
  • 2,056
  • 4
  • 24
  • 47
  • "it's not a permission issue" -- for your app, maybe. It is not safe anymore to assume that other apps have rights to external storage, particularly on Android 6.0+ devices. Hence, the `FileProvider` approach that ianhanniballake points you to. – CommonsWare Aug 19 '16 at 18:01
  • I guess my comment was that since `adb` has access, there doesn't seem to be anything preventing - at least on OS level. In any case, `FileProvider` certainly solves the problem, so thanks @ianhanniballake – Melllvar Aug 19 '16 at 18:17

0 Answers0