ファイルをダウンロードしたいのですが、最初からダウンロードが開始されません。logcat でエラーが見つかりませんでしたが、今は見つかりました。問題は、ファイルを書き込む権限がないためです。
これが私のコードです:
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);
}
}
これは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)