简体   繁体   English

如何从内部存储文件安装应用程序

[英]how to install an app from internal storage files

I'm trying to download an apk file then install it. 我正在尝试下载apk文件然后安装它。
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 这是OnCreate方法的代码

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. DownloadTask是一个从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. 尝试使用openfileoutput()而不是OutputStream将文件保存在内部存储中并允许其可读。 http://developer.android.com/guide/topics/data/data-storage.html#filesInternal . 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. 这是因为android应用程序无法从另一个应用程序文件读取,如果它是使用PRIVATE模式编写的。所以你必须改变文件的模式。 i have just modify else part of your onPostExecute() method below. 我刚刚修改了下面的onPostExecute()方法的其他部分。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将Android App安装到内部sdcard或如何利用内部存储 - How to install Android App to internal sdcard or how to utilize the internal storage 强制将应用安装在内部存储上 - Force app to install on internal storage 如何在内部文件目录中运行安装应用程序? - How to run install app in internal files dir? 如何从内部存储读取文件 - how to read files from Internal Storage 如何在Android手机内部存储上写入.apk文件,以便我可以通过编程方式从内部空间安装该应用 - How to write .apk file on android phone internal storage, so that I can install that app from internal space programatically 如何限制对cordova android内部存储中的应用程序文件的访问? - How to restrict access to app files in internal storage in cordova android? Android,如何防止卸载应用程序时删除内部存储文件 - Android, how to prevent internal storage files to be deleted when the app is uninstalled 如何从URL下载内部存储中的apk然后在android中自动安装? - How to download apk in internal storage from URL then install automatically in android? 如何在android中不允许的情况下从内部存储安装应用程序? - How to install an apps from internal storage while being not allowed in android? Android - 如何从内部存储中获取所有文件路径或从内部存储中删除所有文件 - Android - How to get all file path from internal storage or delete all files from internal storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM