简体   繁体   中英

how to install an app from internal storage files

I'm trying to download an apk file then install it.
I have done it with an external storage directory but when I download the file in a Local directory I can't parse it.

Here is the code on OnCreate method

final DownloadTask downloadTask = new DownloadTask(this);       
downloadTask.execute("http://file.appsapk.com/wp-content/uploads/apps-2/Gmail.apk","Gmail.apk");

DownloadTask is a class that extends from AsyncTask. Here is the background task:

@Override
    protected String doInBackground(String... sUrl) {
        file_name=sUrl[1];
        Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
        if (isSDPresent) {
            directory = new File(Environment.getExternalStorageDirectory()+File.separator+"app_directory");
        }
        else
        {
            directory = getFilesDir();

        }
        if (!directory.exists()) 
            directory.mkdirs();
        InputStream input = null;
        OutputStream output = null;
        HttpURLConnection connection = null;
        try {
            URL url = new URL(sUrl[0]);
            connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            if (connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
                return "Server returned HTTP " + connection.getResponseCode()
                        + " " + connection.getResponseMessage();
            }

            int fileLength = connection.getContentLength();
            input = connection.getInputStream();
            output = new FileOutputStream(directory+"/"+file_name);
            byte[] buffer = new byte[1024];
            long total = 0;
            int count;
            while ((count = input.read(buffer)) != -1) {
                if (isCancelled()) {
                    input.close();
                    return null;
                }
                total += count;
                if (fileLength > 0) // only if total length is known
                    publishProgress((int) (total * 100 / fileLength));
                output.write(buffer, 0, count);
            }
        } catch (Exception e) {
            return e.toString();
        } finally {
            try {
                if (input != null)
                    input.close();

                if (output != null)
                    output.close();
            } catch (IOException ignored) {
            }

            if (connection != null)
                connection.disconnect();
        }
        return null;
    }

And this is the post execution method that runs after the first one done downloading the file:

   @Override
    protected void onPostExecute(String result) {
        mWakeLock.release();
        mProgressDialog.dismiss();
        if (result != null)
        {
            Toast.makeText(context,"Download error: "+result, Toast.LENGTH_LONG).show();
        }
        else
        {
            Toast.makeText(context, "File downloaded", Toast.LENGTH_SHORT).show();
            File file = new File(directory, file_name);
            Intent promptInstall = new Intent(Intent.ACTION_VIEW)
                .setDataAndType(Uri.fromFile(file),
                        "application/vnd.android.package-archive");
           context.startActivity(promptInstall);
           finish();

        }

It runs perfectly with an external storage but it won't run with an external one. Why not?

Try to use openfileoutput() rather than OutputStream in saving your file in internal storage and allow it to be readable. http://developer.android.com/guide/topics/data/data-storage.html#filesInternal . The package error is mainly caused by the permission of internal storage.

It's because of android application can not read from another application file if it is written using PRIVATE mode.So you have to change the mode of the file. i have just modify else part of your onPostExecute() method below.

    try {
        String tempfile="xyzfile";
        File file = new File(directory, file_name);
        FileInputStream inStream = new FileInputStream(file);
        FileOutputStream fos = context.openFileOutput(tempfile,
                context.MODE_WORLD_WRITEABLE | context.MODE_WORLD_READABLE);

        byte[] buffer = new byte[1024];
        int length;
        // copy the file content in bytes
        while ((length = inStream.read(buffer)) > 0) {

            fos.write(buffer, 0, length);

        }
        inStream.close();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
    File file = new File(context.getFilesDir(), tempfile);
    Intent promptInstall = new Intent(Intent.ACTION_VIEW).setDataAndType(
            Uri.fromFile(file), "application/vnd.android.package-archive");
    context.startActivity(promptInstall);

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