Okay, I've been going over this for a while, and I'm not getting it. none of the suggestions in multiple links has solved the problem.
I have successfully set up my Application to send files. currently, I am trying to work on opening those files with my application.
My activity opens when I click on the file in the received email.
What I need it to do next is to save the file locally, in a the external storage folder that my application uses.
So, when you click on a file and the intent filter opens the correct activity, what do you do next to access that file?
intent filter:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
<data android:pathPattern=".*\\.gmgt" />
</intent-filter>
Activity:
public class ActFileReceiver extends Activity {
private TextView label;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file_receiver);
label = (TextView) findViewById(R.id.lblFileRecieverText);
}
}
intent used to send file:
public void emailFile(File file) {
Uri fileURI = Uri.fromFile(file);
Intent mailIntent = new Intent(android.content.Intent.ACTION_SEND);
mailIntent.setType("messsage/vnd.com.boardmonkey.TABLETop.gamefile");
mailIntent.putExtra(Intent.EXTRA_SUBJECT, "TABLETop game file: " + file.getName());
mailIntent.putExtra(Intent.EXTRA_STREAM, fileURI);
startActivity(Intent.createChooser(mailIntent, "Send Mail With..."));
}
You are looking to support ACTION_VIEW
. If you look at the documentation for ACTION_VIEW
, you will find:
Input: getData() is URI from which to retrieve data.
Here, "input" is referring to the data sent from whoever called startActivity()
to you, the one with the activity that the user chose. And, methods referenced in Intent
action documentation refer to the Intent
itself.
So, call getIntent()
to retrieve the Intent
that was used to create your activity, and call getData()
on it to get a Uri
to the content that you are supposed to view.
Now you start running into problems, though. Specifically, you are trying to use android:pathPattern
. Your use has two issues:
You cannot use android:pathPattern
without also specifying android:scheme
and android:host
.
It's 2017, and few things use file extensions anymore. In particular, most content is shared via Uri
values with content
schemes, and a content
Uri
is unlikely to have a file extension.
The only semi-effective way to use android:pathPattern
is to limit yourself to the file
scheme (via android:scheme
), then live with the fact that your app will interoperate with a steadily decreasing number of apps over time.