私は、ダウンロードするために私のWebサーバーからzipファイルをダウンロードしようとしています。AndroidダウンロードZip(サーバー上の名前を変更することができます)
私はzipファイルへの静的リンクを使用してダウンロードする場合は、それは素晴らしい作品が、私はこのコードでPHPファイル使用してダウンロードしようとしている:私は私の中でPHPファイルにアクセスする場合
function file_list($d,$x){
foreach(array_diff(scandir($d,1),array('.','..')) as $f)if(is_file($d.'/'.$f)&&(($x)?ereg($x.'$',$f):1))$l[]=$f;
return $l;
}
$arr = file_list("../download/",".zip");
$filename = $arr[0];
$filepath = "../download/".$arr[0];
if(!file_exists($filepath)){
die('Error: File not found.');
} else {
// Set headers
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
readfile($filepath);
}
をブラウザ、それはちょうど素晴らしいzipをダウンロードします!
さて、Androidの側で、それはなっているように、それはダウンロードが、ジッパーの名前はgetZip.php(PHPファイル名)であり、ファイルサイズが22
これは、ダウンロードを行うコードでありますAndroid上のもの
int count;
try {
downloadCoords();
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lengthOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Length of file: " + lengthOfFile);
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
if(!f.isDirectory()){
f.mkdirs();
}
Uri u = Uri.parse(url.toString());
File uf = new File(""+u);
zipname = uf.getName();
Log.d("ANDRO_ASYNC", "Zipname: " + zipname);
File zipSDCARD = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+zipname);
if(!zipSDCARD.isFile()){
Log.d("zipSDCARD.isFile()","false");
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/sdcard/" + zipname);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lengthOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
}
successDownload = true;
} catch (Exception e) {
successDownload = false;
Log.e("Error","DownloadZip",e);
}
zipnameとziplengthも正しく設定されていますか?
ありがとうございます。
あなたの状況に該当する場合は、[DownloadManager](http://developer.android.com/reference/android/app/DownloadManager.html)を利用することができます。これはあなたのために(再試行などの)ダウンロードのすべての要塞のものを処理します。 – Renard
@レナード私は本当に助けになるかどうかわかりませんが、まったく同じことを達成すると思います... – silentw