android原生开发,从http下载图片,下载失败或成功提示,并放图片显示出来。
1个回答
展开全部
下载类
public class DownFile
{
public InputStream getInput(String path)
{
InputStream in = null;
try {
URL url = new URL(path);
HttpURLConnection hcon = (HttpURLConnection) url.openConnection();
in = hcon.getInputStream();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return in;
}
public int downIamge(String path,String name)
{
InputStream in = getInput(path);
int type = 0;
File ex = Environment.getExternalStorageDirectory();
try {
FileOutputStream out = new FileOutputStream(new File(ex.getAbsoluteFile()+File.separator+name));
int len = 0;
byte[] bb = new byte[1024];
while((len = in.read(bb))!=-1)
{
out.write(bb,0,len);
}
out.close();
type = 1;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
type = 2;
}
return type;
}
}
2在另外一个activity里面调用这个方法DownFile().downIamge(path,name); 返回1就是下载成功 ,2 就显示下载失败
自己手写的 望采纳 不懂 可继续追问
public class DownFile
{
public InputStream getInput(String path)
{
InputStream in = null;
try {
URL url = new URL(path);
HttpURLConnection hcon = (HttpURLConnection) url.openConnection();
in = hcon.getInputStream();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return in;
}
public int downIamge(String path,String name)
{
InputStream in = getInput(path);
int type = 0;
File ex = Environment.getExternalStorageDirectory();
try {
FileOutputStream out = new FileOutputStream(new File(ex.getAbsoluteFile()+File.separator+name));
int len = 0;
byte[] bb = new byte[1024];
while((len = in.read(bb))!=-1)
{
out.write(bb,0,len);
}
out.close();
type = 1;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
type = 2;
}
return type;
}
}
2在另外一个activity里面调用这个方法DownFile().downIamge(path,name); 返回1就是下载成功 ,2 就显示下载失败
自己手写的 望采纳 不懂 可继续追问
更多追问追答
追答
downIamge(path,name); 后面的第二个参数是 下载后图片的名字 我在下载方法里面已经把路径写好了 直接下载到SDcard里面 你要看看怎么写的 不要只知道复制粘贴,切勿拿来主义,只有自己的最终才是自己的。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询