gpt4 book ai didi

android - 如何从 url 下载文件/图像到你的 android 应用程序

转载 作者:行者123 更新时间:2023-11-29 00:40:24 25 4
gpt4 key购买 nike

我需要我的 android 应用程序向 url 发出请求,以从此 url 下载图像所以我建立了这个类来帮助我,但它没有用 ???

public class MyAsnyc extends AsyncTask<Void, Void, Void> {
public static File file;
InputStream is;

protected void doInBackground() throws IOException {
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
file = new File(path, "DemoPicture.jpg");

try{
// Make sure the Pictures directory exists.
path.mkdirs();

URL url = new URL("http://androidsaveitem.appspot.com/downloadjpg");

// Open a connection to that URL.
URLConnection ucon = url.openConnection();

// Define InputStreams to read from the URLConnection.
is = ucon.getInputStream();
} catch (IOException e) {
Log.d("ImageManager", "Error: " + e);
}
}

@Override
protected Void doInBackground(Void... params) {
try {
doInBackground();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

protected void onPostExecute() {
try {
OutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();

// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(
null,
new String[] { file.toString() },
null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
}
);
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

我在 onclick() 的 Activity 类中有这个函数:

public void down(View v) {
// ImageManager ob=new ImageManager();
// ob.DownloadFromUrl("");

new MyAsnyc().execute();
}

虽然我在manfiest.xml里面写了权限

<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

最佳答案

试试这个

public class MyAsnyc extends AsyncTask<Void, Void, Void> {
public static File file;
InputStream is;

protected void doInBackground() throws IOException {

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
file = new File(path, "DemoPicture.jpg");
try {
// Make sure the Pictures directory exists.
path.mkdirs();

URL url = new URL("http://androidsaveitem.appspot.com/downloadjpg");
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();

/*
* Define InputStreams to read from the URLConnection.
*/
is = ucon.getInputStream();

OutputStream os = new FileOutputStream(file);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();

} catch (IOException e) {
Log.d("ImageManager", "Error: " + e);
}
}

@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
try {
doInBackground();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return null;
}

protected void onPostExecute() {
try {
// Tell the media scanner about the new file so that it is
// immediately available to the user.
MediaScannerConnection.scanFile(null,
new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

关于android - 如何从 url 下载文件/图像到你的 android 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9762057/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com