简体   繁体   中英

Intent to install APK in Android

I Try to install apk from file in android but the output is:

There is a Problem Parsing the Package

If i try with the android explorer the APK install.

My code:

  public void instalarPath() {
    try {
        File file = new File(Environment.getExternalStorageState() + "/downloadedfile.apk");
       mensaje(file.getName());//File existis
       Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file), "application/vnd.andriod.package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    } catch (Exception e) {
        mostrarcasoderrror(e.toString(), e.toString());
    }
}

The manifest

  <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Shows:

ANDROID.CONTENTE ACTIVIY NOTFOUND EXCEPTION TO HANDLE INTENTE {ACT=android.intent.action.view dat=file:///mounted/donwloadedfile.apk type=application/}

mensaje(file.getName());//File existis

No, that file does not exist.

File file = new File(Environment.getExternalStorageState() + "/downloadedfile.apk");

That is not a valid path on any Android device manufactured in human history. getExternalStorageState() will return a value like unmountable , not some filesystem directory.

This:

File file = new File(Environment.getExternalStorageState() + "/downloadedfile.apk"

Is not doing what you are assuming it is doing, as @CommonsWare has mentioned.

Assuming you have said file in the device storage, you want to use: Environment.getExternalStorageDirectory() or Environment.getExternalStoragePublicDirectory() or even getExternalCacheDir() if your download routine supports it.

I resolved using MimeTypeMap mime = MimeTypeMap.getSingleton();

  File file = new File(llegada);
        mensaje(Uri.fromFile(file).getEncodedPath());//File existis
        Intent intent = new Intent();
        intent.setAction(android.content.Intent.ACTION_VIEW);
       // File file = new File(path);

        MimeTypeMap mime = MimeTypeMap.getSingleton();
        String ext=file.getName().substring(file.getName().indexOf(".")+1);
        String type = mime.getMimeTypeFromExtension(ext);

        intent.setDataAndType(Uri.fromFile(file), type);

        startActivity(intent);

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