从URL保存图片到SD卡 [英] Save image from url to sdcard
本文介绍了从URL保存图片到SD卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是从网址保存图像到SD卡。但是,图像尺寸为SD卡0。图像在SD卡创建,但现在检索URL数据和保存。所以这是给我0的大小。
尝试
{
网址URL =新的URL(http://api.androidhive.info/images/sample.jpg);
输入的InputStream = url.openStream();
尝试{
//该SD卡目录例如'/ SD卡,可直接使用,或
//与getExternalStorageDirectory更安全抽象()
文件storagePath = Environment.getExternalStorageDirectory();
OutputStream的输出=新的FileOutputStream(新文件(storagePath,用户名+PNG));
尝试{
字节[]缓冲区=新的字节[2048];
INT读取动作= 0;
而((读取动作= input.read(缓冲液,0,buffer.length))GT; = 0){
output.write(缓冲液,0,读取动作);
}
} {最后
output.close();
}
} {最后
input.close();
}
}赶上(例外五)
{
的System.out.println(错误SD卡+ e.toString());
}
解决方案
试试这个code.It作品...
您应该有互联网的权限和写入外部存储设备。
尝试
{
网址URL =新的URL(中输入网址,下载);
HttpURLConnection类的URLConnection =(HttpURLConnection类)url.openConnection();
urlConnection.setRequestMethod(GET);
urlConnection.setDoOutput(真);
urlConnection.connect();
文件SDCardRoot = Environment.getExternalStorageDirectory()getAbsoluteFile()。
字符串文件名=downloadedFile.png;
Log.i(本地文件名:,+文件名);
档案文件=新的文件(SDCardRoot,文件名);
如果(file.createNewFile())
{
file.createNewFile();
}
FileOutputStream中fileOutput =新的FileOutputStream(文件);
为InputStream的InputStream = urlConnection.getInputStream();
INT总计TOTALSIZE = urlConnection.getContentLength();
INT downloadedSize = 0;
字节[]缓冲区=新的字节[1024];
INT BufferLength中= 0;
而(量(bufferLength = inputStream.read(缓冲液))大于0)
{
fileOutput.write(缓冲液,0,BufferLength中);
downloadedSize + = BufferLength中;
Log.i(进步,downloadedSize:+ downloadedSize +总计TOTALSIZE:+总计TOTALSIZE);
}
fileOutput.close();
如果(downloadedSize ==总计TOTALSIZE)文件路径= file.getPath();
}
赶上(MalformedURLException的E)
{
e.printStackTrace();
}
赶上(IOException异常E)
{
文件路径= NULL;
e.printStackTrace();
}
Log.i(文件路径:,+文件路径);
返回文件路径;
I am saving image from url to sdcard. But image size is 0 in sdcard. Image is created in sdcard but now retrieve data from url and save. so it is giving me 0 size.
try
{
URL url = new URL("http://api.androidhive.info/images/sample.jpg");
InputStream input = url.openStream();
try {
//The sdcard directory e.g. '/sdcard' can be used directly, or
//more safely abstracted with getExternalStorageDirectory()
File storagePath = Environment.getExternalStorageDirectory();
OutputStream output = new FileOutputStream (new File(storagePath,username+".png"));
try {
byte[] buffer = new byte[2048];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, bytesRead);
}
} finally {
output.close();
}
} finally {
input.close();
}
} catch(Exception e)
{
System.out.println("error in sd card "+e.toString());
}
解决方案
Try this code.It works...
You should have permission of internet and write external storage.
try
{
URL url = new URL("Enter the URL to be downloaded");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
File SDCardRoot = Environment.getExternalStorageDirectory().getAbsoluteFile();
String filename="downloadedFile.png";
Log.i("Local filename:",""+filename);
File file = new File(SDCardRoot,filename);
if(file.createNewFile())
{
file.createNewFile();
}
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();
int totalSize = urlConnection.getContentLength();
int downloadedSize = 0;
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ( (bufferLength = inputStream.read(buffer)) > 0 )
{
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
Log.i("Progress:","downloadedSize:"+downloadedSize+"totalSize:"+ totalSize) ;
}
fileOutput.close();
if(downloadedSize==totalSize) filepath=file.getPath();
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
filepath=null;
e.printStackTrace();
}
Log.i("filepath:"," "+filepath) ;
return filepath;
这篇关于从URL保存图片到SD卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文