Androidのダウンロードバイナリファイルの問題
-
05-09-2019 - |
質問
私は、インターネットから私のアプリでは、バイナリファイル(映像)をダウンロードの問題を抱えています。私はそれを直接ダウンロードする場合はQuicktimeでは、それが正常に動作しますが、何とか私のアプリを通じてそれがのは、(彼らはテキストエディタでまったく同じに見えるにもかかわらず)めちゃくちゃます。ここでは例があります:
URL u = new URL("http://www.path.to/a.mp4?video");
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
FileOutputStream f = new FileOutputStream(new File(root,"Video.mp4"));
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ( (len1 = in.read(buffer)) > 0 ) {
f.write(buffer);
}
f.close();
解決
私は知りませんが、あなたはそこに従来のJavaのグリッチを持っている:)あなたは(読み事実にカウントしていないの常にの復帰を許可されていますあなたが求めるよりも少ないバイト。このように、あなたの読み取りが1024未満のバイトを得ることができますが、あなたの書き込みは常に可能性が前のループの反復からのバイトを含む正確に1024バイトを書き出します。
で正しいます:
while ( (len1 = in.read(buffer)) > 0 ) {
f.write(buffer,0, len1);
}
おそらく、高いレイテンシーのネットワーキングやAndroid上の3Gの小さなパケットサイズが効果を悪化されますか?
他のヒント
new DefaultHttpClient().execute(new HttpGet("http://www.path.to/a.mp4?video"))
.getEntity().writeTo(
new FileOutputStream(new File(root,"Video.mp4")));
一つの問題は、バッファのあなたの読書です。入力ストリームのすべての読み取りが1024の倍数でない場合は、不正なデータをコピーします。使用します:
byte[] buffer = new byte[1024];
int len1 = 0;
while ( (len1 = in.read(buffer)) != -1 ) {
f.write(buffer,0, len1);
}
public class download extends Activity {
private static String fileName = "file.3gp";
private static final String MY_URL = "Your download url goes here";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
URL url = new URL(MY_URL);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = Environment.getExternalStorageDirectory()
+ "/download/";
Log.d("Abhan", "PATH: " + PATH);
File file = new File(PATH);
if(!file.exists()) {
file.mkdirs();
}
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.flush();
fos.close();
is.close();
} catch (IOException e) {
Log.e("Abhan", "Error: " + e);
}
Log.i("Abhan", "Check Your File.");
}
}
私はこのスレッドで、以前のフィードバックに基づいてコードを修正しました。私は日食と複数の大きなファイルを使用してテストしました。これは、正常に動作しています。ただコピーして貼り付け、これをご使用の環境にし、HTTPパスとあなたがダウンロードするファイルを希望の場所を変更する必要があります。
try {
//this is the file you want to download from the remote server
String path ="http://localhost:8080/somefile.zip";
//this is the name of the local file you will create
String targetFileName
boolean eof = false;
URL u = new URL(path);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
FileOutputStream f = new FileOutputStream(new File("c:\\junk\\"+targetFileName));
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ( (len1 = in.read(buffer)) > 0 ) {
f.write(buffer,0, len1);
}
f.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
幸運 アリレザAghamohammadi
ただ、Apacheのコピー方法(のApache CommonsのIO を)使用 - Javaを使用しての利点を!
IOUtils.copy(is, os);
finallyブロック内のストリームを閉じることを忘れないでください。
try{
...
} finally {
IOUtils.closeQuietly(is);
IOUtils.closeQuietly(os);
}