2012-04-30 126 views
1

我正在使用setImageBitmap和外部图像url设置ImageView。我想保存图像,以便以后可以使用,即使没有互联网连接。我在哪里以及如何保存它?如何从网址保存图片?

回答

3

您必须将其保存在SD卡或包数据中,因为在运行时您只能访问这些数据。要做到这一点,这是一个很好的例子

URL url = new URL ("file://some/path/anImage.png"); 
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 (storagePath + "/myImage.png"); 
try { 
    byte[] buffer = new byte[aReasonableSize]; 
    int bytesRead = 0; 
    while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) { 
     output.write(buffer, 0, bytesRead); 
    } 
} finally { 
    output.close(); 
} 
} finally { 
input.close(); 
} 

来源:How do I transfer an image from its URL to the SD card?

+0

该代码将无法编译。 'Environment.getExternalStorageDirectory()'不返回一个'String'。 –

+0

更新,现在它会!谢谢 –

+3

什么是合理的尺寸 –

0

你可以在SD卡上保存图像,你可以在未来没有互联网的情况下使用该图像。

see this tutorial将显示如何存储图像并再次读取它。

希望这会帮助你.....!

3
URL imageurl = new URL("http://mysite.com/me.jpg"); 
Bitmap bitmap = BitmapFactory.decodeStream(imageurl.openConnection().getInputStream()); 

此代码将帮助您从图像生成URL的位图。

这个question回答第二部分。

0

可能是它会帮助别人像我一天

new SaveImage().execute(mViewPager.getCurrentItem());//calling function 

private void saveImage(int currentItem) { 
    String stringUrl = Site.BASE_URL + "socialengine/" + allImages.get(currentItem).getMaster(); 
    Utils.debugger(TAG, stringUrl); 

    HttpURLConnection urlConnection = null; 
    try { 
     URL url = new URL(stringUrl); 
     urlConnection = (HttpURLConnection) url.openConnection(); 
     urlConnection.setRequestMethod("GET"); 
     urlConnection.setDoOutput(true); 
     urlConnection.connect(); 
     File sdCardRoot = Environment.getExternalStorageDirectory().getAbsoluteFile(); 

     String fileName = stringUrl.substring(stringUrl.lastIndexOf('/') + 1, stringUrl.length()); 
     String fileNameWithoutExtn = fileName.substring(0, fileName.lastIndexOf('.')); 

     File imgFile = new File(sdCardRoot, "IMG" + System.currentTimeMillis()/100 + fileName); 
     if (!sdCardRoot.exists()) { 
      imgFile.createNewFile(); 
     } 

     InputStream inputStream = urlConnection.getInputStream(); 
     int totalSize = urlConnection.getContentLength(); 
     FileOutputStream outPut = new FileOutputStream(imgFile); 

     int downloadedSize = 0; 
     byte[] buffer = new byte[2024]; 
     int bufferLength = 0; 
     while ((bufferLength = inputStream.read(buffer)) > 0) { 
      outPut.write(buffer, 0, bufferLength); 
      downloadedSize += bufferLength; 
      Utils.debugger("Progress:", "downloadedSize:" + Math.abs(downloadedSize*100/totalSize)); 
     } 
     outPut.close(); 
     //if (downloadedSize == totalSize); 
      //Toast.makeText(context, "Downloaded" + imgFile.getPath(), Toast.LENGTH_LONG).show(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

} 

private class SaveImage extends AsyncTask<Integer, Void, String> { 

    @Override 
    protected String doInBackground(Integer... strings) { 
     saveImage(strings[0]); 
     return "saved"; 
    } 

    @Override 
    protected void onPostExecute(String s) { 
     Toast.makeText(context, "" + s, Toast.LENGTH_SHORT).show(); 
    } 
}