在Android中下载文件时解析错误

时间:2016-12-16 10:10:44

标签: android

从url下载文件时出现此错误(解析错误:解析包时出现问题。)

我用谷歌搜索,他们说安装Android应用程序时出现问题这个消息出现,但我的问题是不同的Android应用程序运行正常,但当我从URL添加代码下载文件并单击下载按钮时出现此错误消息

此消息在某些设备上显示的奇怪之处并非全部!!

这是我从url下载apk文件的代码:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btn = (Button)findViewById(R.id.button);

    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           new DownloadFileFromURL().execute(LATEST_VERSION_URL);
        }
    });
}

和DownloadFileFromURL类:

 /**
 * Background Async Task to download file
 * */
class DownloadFileFromURL extends AsyncTask<String, String, String> {

    /**
     * Before starting background thread
     * Show Progress Bar Dialog
     * */
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progress = ProgressDialog.show(MainActivity.this, "", "Updating, please wait this may take a few minutes...", true);
    }

    /**
     * Downloading file in background thread
     * */
    @Override
    protected String doInBackground(String... f_url) {
        int count;
        try {
            URL url = new URL(f_url[0]);
            URLConnection conection = url.openConnection();
            conection.connect();
            // getting file length
            int lenghtOfFile = conection.getContentLength();

            // input stream to read file - with 8k buffer
            InputStream input = new BufferedInputStream(url.openStream(), 8192);

            // Output stream to write file
            OutputStream output = new FileOutputStream("/sdcard/myapp.apk");

            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
                total += count;
                output.write(data, 0, count);
            }

            // flushing output
            output.flush();

            // closing streams
            output.close();
            input.close();

        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }

        return null;
    }

    /**
     * Updating progress bar
     * */
    protected void onProgressUpdate(String... progress) {
    }

    /**
     * After completing background task
     * Dismiss the progress dialog
     * **/
    @Override
    protected void onPostExecute(String file_url) {
        progress.dismiss();
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "myapp.apk")), "application/vnd.android.package-archive");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
        startActivity(intent);
    }

任何人都可以帮助我,我已经解决了这个问题超过2天

感谢。

更新我解决了它:)

首先问题出现在Android 6(Marshmallow)中,即使用户在安装时接受了您的所有权限,他们以后也可以决定从您那里获取一些权限,以便抛出

java.io.IOException:open failed:EACCES(Permission denied)

所以在上面的代码中,异常抛出并捕获它然后转到 onPostExecute 并尝试打开apk文件并安装它,但实际上文件没有下载因为这个异常。< / p>

0 个答案:

没有答案