如何安装从内部存储文件的应用程序 [英] how to install an app from internal storage files

查看:160
本文介绍了如何安装从内部存储文件的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试下载APK文件再进行安装。结果
我已经与外部存储目录做了,但是当我下载的文件的本地目录,我不能分析它。

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.

下面是onCreate方法的code

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是,从AsyncTask的延伸的类。
这里是后台任务:

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?

推荐答案

尝试在保存文件中的内部存储使用openfileoutput(),而不是为OutputStream,并允许它是可读的。 http://developer.android.com/guide/topics/data /data-storage.html#filesInternal 。包错误主要是由于内部存储的许可。

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆