我得到的图像链接从服务器并在其上应该下载并保存到画廊中的链接的点击,我对如何做到这一点不知道。请帮我在这,在此先感谢
**combine the below code to store image at a particula path**
File storagePath = new File(Environment
.getExternalStorageDirectory()
+ "/com.logistics.herestethiparty/images/");
storagePath.mkdirs();
File file = new File(storagePath, fileImageName);
**i have used the below code in asyncclass for image download**
URL url = new URL(imageUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
FileOutputStream fileOutput = new FileOutputStream(file);
InputStream inputStream = urlConnection.getInputStream();
int downloadedSize = 0;
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
}
// close the output stream when done
fileOutput.close();
试试这个代码,调用mDownloadAndSave()
方法节省SD卡的图像,这将解决您的问题。
public void mDownloadAndSave() {
// Setting up file to write the image to.
File f = new File("/mnt/sdcard/img.png");
// Open InputStream to download the image.
InputStream is;
try {
is = new URL("http://www.tmonews.com/wp-content/uploads/2012/10/androidfigure.jpg").openStream();
// Set up OutputStream to write data into image file.
OutputStream os = new FileOutputStream(f);
CopyStream(is, os);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void CopyStream(InputStream is, OutputStream os) {
final int buffer_size = 1024;
try {
byte[] bytes = new byte[buffer_size];
for (;;) {
int count = is.read(bytes, 0, buffer_size);
if (count == -1)
break;
os.write(bytes, 0, count);
}
} catch (Exception ex) {
}
}
而且不要忘了下面的权限加入到AndroidManifest.xml中
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>