2012-04-30 4 views

답변

3

SD 카드 또는 패키지 데이터로 저장해야합니다. 런타임에는 사용자가 액세스 할 수있는 권한 만 있기 때문입니다. 이 코드는 이미지 URL에서 비트 맵을 생성에 도움이 될 것입니다 How do I transfer an image from its URL to the SD card?

+0

일일 도움이 될 수 있음. 'Environment.getExternalStorageDirectory()'는'String'을 리턴하지 않습니다. –

+0

가 업데이트되었습니다. 감사합니다 –

+3

무엇입니까 aReasonableSize –

0

당신은 sdcard에 이미지를 저장할 수 있으며 인터넷 없이는 미래에 그 이미지를 사용할 수 있습니다.

see this tutorial은 이미지를 저장하고 다시 읽는 방법을 보여줍니다.

희망이 도움이 될 것입니다 .....!

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

:이

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(); 
} 

소스 좋은 예에게 있음을 수행합니다.

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(); 
    } 
} 
관련 문제