When downloading the apk in the automatic app update module, you may encounter two problems: internal storage and SD card storage. The apk stored in the sk card can be installed normally, however, parse error occurs when apk is installed in the internal storage.
You can search for it online in two ways:
1. Set permissions for files during storage
2. Change the File Permission before using the file
At first, the idea was not clarified, so I started to try it. After many attempts, the problem was still not solved, and I started to analyze it a little after I consulted Daniel.
First, read and write common files.
File apkFile = new File(mSavePath, appName); FileOutputStream fos = new FileOutputStream(apkFile);
Then use solution 2:
String chmodCmd = "chmod 666 " + apkfile.getAbsolutePath(); try { Runtime.getRuntime().exec(chmodCmd); } catch (Exception e) { } Intent i = new Intent(Intent.ACTION_VIEW); String filePath = "file://" + apkfile.toString(); i.setDataAndType(Uri.fromFile(apkfile),"application/vnd.android.package-archive"); mContext.startActivity(i);
The problem is solved.
Looking back, I was surprised when I looked at the file. The named file was downloaded, but the file size was 0 after the call, fileOutputStream fos = mContext is found. openFileOutput (appName, Context. MODE_WORLD_READABLE | Context. MODE_WORLD_WRITEABLE); used once before saving the file and calling the apk installation code. The openFileOutput method is called again, causing the file content to be cleared, you only need to set the File Permission to read and write when writing files.
String fileName = "tmp.apk ";
FileOutputStream fos = openFileOutput (fileName,
MODE_WORLD_READABLE | MODE_WORLD_WRITEABLE );
// Write the. apk content here... flush () and close ()
// Now start the standard instalation window
File fileLocation = new File (context. getFilesDir (), fileName );
Intent intent = new Intent (Intent. ACTION_VIEW );
Intent. setDataAndType (Uri. fromFile (fileLocation ),
"Application/vnd. android. package-archive ");
Context. startActivity (intent );