本文介绍了从以下网址下载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从网上下载图片::我指的this
I want to download image from internet ::I refer this
和尝试这个code ::
and try this code ::
package com.Downld_file_frm_net;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.util.ByteArrayBuffer;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class Downld_file_frm_net extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//http://203.109.115.55/MRESC/images/150.jpg
DownloadFromUrl("http://nssdc.gsfc.nasa.gov/planetary/image/cassini_titan_15.jpg","cassini_titan_15.jpg");
}
public void DownloadFromUrl(String DownloadUrl, String fileName) {
try {
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/xmls");
if(dir.exists()==false) {
dir.mkdirs();
}
URL url = new URL(DownloadUrl); //you can write here any link
File file = new File(dir, fileName);
long startTime = System.currentTimeMillis();
Log.d("DownloadManager", "download begining");
Log.d("DownloadManager", "download url:" + url);
Log.d("DownloadManager", "downloaded file name:" + fileName);
/* Open a connection to that URL. */
URLConnection ucon = url.openConnection();
/*
* Define InputStreams to read from the URLConnection.
*/
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
/*
* Read bytes to the Buffer until there is nothing more to read(-1).
*/
ByteArrayBuffer baf = new ByteArrayBuffer(5000);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
/* Convert the Bytes read to a String. */
FileOutputStream fos = new FileOutputStream(file);
fos.write(baf.toByteArray());
fos.flush();
fos.close();
Log.d("DownloadManager", "download ready in" + ((System.currentTimeMillis() - startTime) / 1000) + " sec");
} catch (IOException e) {
Log.d("DownloadManager", "Error: " + e);
}
}
}
在emulator.i的SD卡但是,并非店面形象logcat的没有错误genrate我已经给所有这些都是提权限。
But not store image on SDCARD of emulator.i logcat no error genrate i have give all permission which are mention.
推荐答案
更改code :::
File dir = new File (root.getAbsolutePath() + "/xmls");
到
File dir = new File (root.getAbsolutePath() + "/mnt/sdcard");
这篇关于从以下网址下载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!