简体   繁体   中英

Android studio open external file from app

I'm trying to get my app to download and open a word document in whatever word processing app the device has using the below code. However, I keep getting "there was a problem passing the package" when I try and open it. The word document downloads fine, because I can go to files and open it from there. However, when I try and open it from my app it causes the error.

Here is my code for opening the file. I already use it elsewhere in my app to open an APK file and that works fine.

public static void OpenFile(Activity activity, String name){
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Command Tasks.docx")), "application/vnd.android.package-archive");
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    activity.startActivity(intent);
}

DOCX files do not have a MIME type of application/vnd.android.package-archive . That MIME type is for APK files.

Either replace application/vnd.android.package-archive with the proper MIME type for DOCX files, or do not supply a MIME type and hope that the system figures out the correct MIME type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM