简体   繁体   English

未下载Android文件

[英]Android file not downloaded

I want to download a file but the download doesn't start at the beggining i didn't find the errors in the logcat but now i do and the problems are because i don't have the permissions to write a file 我想下载一个文件,但是下载不是从开始开始的,我没有在logcat中找到错误,但是现在我可以了,问题是因为我没有写文件的权限

Here is my code: 这是我的代码:

public class UpdateSystem extends Activity {


    private int progressBarStatus = 0;
    private Handler progressBarHandler = new Handler();
    ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_system);
        download_version task = new download_version();
        progressBar = (ProgressBar) findViewById(R.id.progressBar1);


        Bundle extras = getIntent().getExtras();
        String version = extras.getString("version");


        task.execute(path/"Example.apk");


    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.update_system, menu);
        return true;
    }



    private class download_version extends AsyncTask<String, Integer, String> {
        @Override
        protected String doInBackground(String... sUrl) {
            try {

                URL url = new URL(sUrl[0]);

                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

                urlConnection.setRequestMethod("GET");

                urlConnection.setDoOutput(true);

                urlConnection.connect();

                // this will be useful so that you can show a typical 0-100% progress bar
                int fileLength = urlConnection.getContentLength();

                // download the file
                InputStream input = new BufferedInputStream(url.openStream());
                OutputStream output = new FileOutputStream("/sdcard/Example.apk");

                byte data[] = new byte[1024];
                long total = 0;
                int count;



                while ((count = input.read(data)) != -1) {
                    total += count;
                    // publishing the progress....
                    publishProgress((int) (total * 100 / fileLength));
                    output.write(data, 0, count);
                }

                output.flush();
                output.close();
                input.close();
                return "0";
            } catch (Exception e) {
                Log.d("Error en el sistema", e.getMessage());
                return "1";
            }

        }

        @Override
        protected void onProgressUpdate(Integer... progress) {
            super.onProgressUpdate(progress);
            TextView progress_lbl = (TextView)findViewById(R.id.progress_lbl); 
            progress_lbl.setText("");
            progress_lbl.setText("Progreso de la descarga: "+progress[0].toString()+"%");
            progressBar.setProgress(progress[0]);
        }

        @Override
        protected void onPostExecute(String result){
            installApk();
        }

    }


     private void installApk(){
            Intent intent = new Intent(Intent.ACTION_VIEW);
            Uri uri = Uri.fromFile(new File("/sdcard/Example.apk"));
            intent.setDataAndType(uri, "application/vnd.android.package-archive");
            startActivity(intent);
            System.exit(0);
        }

}

This is the logcat: 这是logcat:

04-29 15:54:01.142: D/Error en el sistema(418): /sdcard/Favai.apk (Permission denied)
04-29 15:54:01.142: D/Error en el sistema(418): java.io.FileNotFoundException: /sdcard/Favai.apk (Permission denied)
04-29 15:54:01.142: D/Error en el sistema(418):     at org.apache.harmony.luni.platform.OSFileSystem.openImpl(Native Method)
04-29 15:54:01.142: D/Error en el sistema(418):     at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:152)
04-29 15:54:01.142: D/Error en el sistema(418):     at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
04-29 15:54:01.142: D/Error en el sistema(418):     at java.io.FileOutputStream.<init>(FileOutputStream.java:168)
04-29 15:54:01.142: D/Error en el sistema(418):     at java.io.FileOutputStream.<init>(FileOutputStream.java:147)
04-29 15:54:01.142: D/Error en el sistema(418):     at srm.favai.UpdateSystem$download_version.doInBackground(UpdateSystem.java:79)
04-29 15:54:01.142: D/Error en el sistema(418):     at srm.favai.UpdateSystem$download_version.doInBackground(UpdateSystem.java:1)
04-29 15:54:01.142: D/Error en el sistema(418):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-29 15:54:01.142: D/Error en el sistema(418):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-29 15:54:01.142: D/Error en el sistema(418):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-29 15:54:01.142: D/Error en el sistema(418):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
04-29 15:54:01.142: D/Error en el sistema(418):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
04-29 15:54:01.142: D/Error en el sistema(418):     at java.lang.Thread.run(Thread.java:1096)

You need to add this permission 您需要添加此权限

android.permission.READ_EXTERNAL_STORAGE

Read External Storage 读取外部存储

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM