我正在尝试从URL下载文件。我的代码没有返回错误,但我看不到我应该在我的内部存储中下载的文件。下面是我的代码:
package com.example.downloadfile;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.TextView;
public class DownloadFile extends Activity {
private static String fileName = "al.jpg";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = new TextView(this);
tv.setText("This is download file program... ");
try {
URL url = new URL("http://www.fullissue.com/wp-content/uploads/2010/12/Adam-Lambert.jpg");
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = Environment.getDataDirectory() + "/";
tv.append("nPath > " + PATH);
Log.v("log_tag", "PATH: " + PATH);
File file = new File(PATH);
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.close();
is.close();
} catch (IOException e) {
Log.d("log_tag", "Error: " + e);
}
Log.v("log_tag", "Check: ");
tv.append("nAnother append!");
this.setContentView(tv);
}
}
我是新的java和android开发,任何答案将非常感激,谢谢!
哟!我用了ff。代码来代替。这对我很有效。谢谢你的帮助!
private static String fileName = "beautiful_galaxy - tarantula.jpg";
private static String fileURL = "http://apod.nasa.gov/apod/image/0903/tarantula2_hst_big.jpg";
try {
File root = Environment.getExternalStorageDirectory();
URL u = new URL(fileURL);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
int lenghtOfFile = c.getContentLength();
FileOutputStream f = new FileOutputStream(new File(root + "/download/", fileName));
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
long total = 0;
while ((len1 = in.read(buffer)) > 0) {
total += len1; //total = total + len1
//publishProgress("" + (int)((total*100)/lenghtOfFile));
f.write(buffer, 0, len1);
}
f.close();
} catch (Exception e) {
Log.d("Downloader", e.getMessage());
}
如果您正在下载文件到Sdcard,那么请确保您的Sdcard已挂载,代码将下载文件到Sdcard,如果仍然有问题请告诉我。
<>之前尝试{URL =新URL(提供任何URL);HttpURLConnection c = (HttpURLConnection) url.openConnection();c.setRequestMethod("获得");c.setDoOutput(真正的);c.connect ();String PATH = Environment.getExternalStorageDirectory()+"/下载/";日志。v(LOG_TAG, "PATH: " + PATH);File File = new File(PATH);file.mkdirs ();String fileName = "Test.mp3";文件输出文件=新文件(文件,文件名);FileOutputStream fos = new FileOutputStream(outputFile);InputStream is = c.getInputStream();Byte [] buffer = new Byte [1024];Int len1 = 0;While ((len1 = is.read(buffer)) != -1) {安全系数。Write (buffer, 0, len1);}fos.close ();is.close ();//}} catch (IOException e) {日志。d(LOG_TAG, "Error: " + e);吐司。makeText(myApp, "error " + e.toString(), Toast.LENGTH_LONG),告诉();}之前祝你好运
他说应该用Apache HTTP客户端而不是java客户端
下面是取自教程的代码片段:
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://localhost/");
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
int l;
byte[] tmp = new byte[2048];
while ((l = instream.read(tmp)) != -1) {
}
}
非常确定(就像@dmon所说的),您不能以这种方式写入数据目录。你想:
- 使用
Environment.getDownloadCacheDirectory ()
或Environment.getExternalStorageDirectory ()
。看一下http://developer.android.com/reference/android/os/Environment.html#getDataDirectory%28%29的javadoc。 - 使用URLConnection()很好:这是标准的java,工作得很好。
- android日志怎么说?
检查Manifest文件中的以下权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
您现在将有一个NetworkOnMainThreadException。看看那只木头猫。把你的internet代码放到asynctask或线程中
使用以下代码下载文件:
public boolean DownloadFile(String url, File outputFile)
{
try {
URL u = new URL(url);
URLConnection conn = u.openConnection();
int contentLength = conn.getContentLength();
DataInputStream stream = new DataInputStream(u.openStream());
byte[] buffer = new byte[contentLength];
stream.readFully(buffer);
stream.close();
DataOutputStream fos = new DataOutputStream(new FileOutputStream(outputFile));
fos.write(buffer);
fos.flush();
fos.close();
}
catch(FileNotFoundException e)
{
return false;
}
catch (IOException e)
{
return false;
}
return true;
}