0

I have two apps, one is writing the file and other one (Gmail) is trying to attach that file. But now, the Gmail app gives the error of

Permission denied for attaching the file.

I think I am storing the file in private directory that is /data/data/PACKAGE/files/file.txt Where can i write the file so that Gmail app not give the error? I am attaching the file as;

String filelocation = "/data/data/com.example.xxxxxxxxx/files/file.txt";
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setType("text/plain");
String message="File to be shared is " + file_name + ".";
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse( "file://"+filelocation));
intent.putExtra(Intent.EXTRA_TEXT, message);
intent.setData(Uri.parse("mailto:xyz@gmail.com"));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Humty
  • 1,321
  • 3
  • 18
  • 35
  • @ianhanniballake I have added the tag but `android:name="android.support.v4.context.FileProvider"` gives the error that 'Unresolved package 'context'' – Humty Apr 10 '17 at 04:53
  • That's because it is `content`, not `context`. – ianhanniballake Apr 10 '17 at 04:54
  • @ianhanniballake Noe `.FileProvider` is red. and giving `Unresolved class`. – Humty Apr 10 '17 at 04:57
  • It sounds like you didn't read [sharing files training](https://developer.android.com/training/secure-file-sharing/setup-sharing.html) in the answer and see the Note in the first section. – ianhanniballake Apr 10 '17 at 05:00
  • @ianhanniballake Kindly see the thread, I started it, facing a problem. http://stackoverflow.com/questions/43316201/cannot-attach-empty-file-using-file-provider – Humty Apr 10 '17 at 06:24
  • @ianhanniballake Kindly.http://stackoverflow.com/questions/43316201/cannot-attach-empty-file-in-gmail-using-file-provider?noredirect=1#comment73697247_43316201 – Humty Apr 10 '17 at 06:45

1 Answers1

0

Instead of using static file path, I recommend to use Environment.getExternalStorageDirectory().getAbsolutePath() that will return you directory up to your public storage. Then you can make static path up to your private folder and merge with /filename.extension.

Chintan Joshi
  • 1,207
  • 1
  • 9
  • 17