我正在开发图像android app有没有办法使用以下代码将图像从服务器保存到SD卡。它的工作,但获得第一个图像只有当第二个图像循环执行协议没有找到错误得到。没有得到所有图像如何获得所有图像
this is my 1st URL,2nd URL
for (int i = 0; i < merchant_details.length(); i++) {
JSONObject c = merchant_details.getJSONObject(i);
URL url = new URL (l.getString("merchant_image"));
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,"myimage.png"));
try {
byte[] buffer = new byte[2024];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, bytesRead);
}
} finally {
output.close();
}
} finally {
input.close();
}
en04-16 13:47:01.128 2664-2665/? D/dalvikvm﹕ GC_CONCURRENT freed 741K, 7% free 12694K/13575K, paused 13ms+1ms, total 19ms
04-16 13:47:01.212 2664-2680/? W/System.err﹕ java.net.MalformedURLException: Protocol not found:
04-16 13:47:01.212 2664-2680/? W/System.err﹕ at java.net.URL.<init>(URL.java:178)
04-16 13:47:01.216 2664-2680/? W/System.err﹕ at java.net.URL.<init>(URL.java:127)
04-16 13:47:01.216 2664-2680/? W/System.err﹕ at emenu.lists.order.Home$GetMerchant_details.doInBackground(Home.java:316)
04-16 13:47:01.216 2664-2680/? W/System.err﹕ at emenu.lists.order.Home$GetMerchant_details.doInBackground(Home.java:275)
04-16 13:47:01.216 2664-2680/? W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:287)
04-16 13:47:01.216 2664-2680/? W/System.err﹕ at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
04-16 13:47:01.216 2664-2680/? W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:137)
04-16 13:47:01.216 2664-2680/? W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
04-16 13:47:01.216 2664-2680/? W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
04-16 13:47:01.216 2664-2680/? W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
04-16 13:47:01.216 2664-2680/? W/System.err﹕ at java.lang.Thread.run(Thread.java:856)
04-16 13:47:04.868 293-320/? E/Genymotion﹕ Could not open '/sys/class/power_supply/genymotion_fake_path/present'
04-16 13:47:09.868 293-320/? E/Genymotion﹕ Could not open '/sys/class/power_supply/genymotion_fake_path/present'
04-16 13:47:14.868 293-320/? E/Genymotion﹕ Could not open '/sys/class/power_supply/genymotion_fake_path/present'
04-16 13:47:19.868 293-320/? E/Genymotion﹕ Could not open '/sys/class/power_supply/genymotion_fake_path/present'
04-16 13:47:24.868 293-320/? E/Genymotion﹕ Could not open '/sys/class/power_supply/genymotion_fake_path/present'
04-16 13:47:29.868 293-320/? E/Genymotion﹕ Could not open '/sys/class/power_supply/genymotion_fake_path/present'
04-16 13:47:34.872 293-320/? E/Genymotion﹕ Could not open '/sys/class/power_supply/genymotion_fake_path/present'
04-16 13:47:39.872 293-320/? E/Genymotion﹕ Could not open '/sys/class/power_supply/genymotion_fake_path/present'
04-16 13:4
答案 0 :(得分:0)
您从l.getString("merchant_image")
获得的是网络地址。但是您试图访问您没有从互联网上下载的文件。如果您尝试将此文件下载到本地存储系统,请查看以下内容:
try {
File f = new File("yourfilename.mp3");
if (f.exists()) {
publishProgress(100,100);
} else {
int count;
URL url = new URL("http://site:port/your/mp3file/here.mp3");
URLConnection connection = url.openConnection();
connection.connect();
int lengthOfFile = connection.getContentLength();
long total = 0;
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(f);
byte data[] = new byte[1024];
while ((count = input.read(data)) != -1) {
total += count;
publishProgress((int)(total/1024),lengthOfFile/1024);
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
}
} catch (Exception e) {
Log.e("Download Error: ", e.toString());
}
我希望你的灵感来自我的代码。