Install apk for android internal storage

Source: Internet
Author: User
Tags parse error

 

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 );

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.