2012-05-08 4 views
-1

이미지 형식 URL을 다운로드 할 때 다음 코드가 유용합니다. 버튼 클릭시 imageview에로드 된 이미지를 sdcard에 저장하고 싶습니다. 아무도 도와주세요 어떻게 다운로드 한 이미지를 sdcard에 저장할 수 있습니까?안드로이드에서 다운로드 한 이미지를 sdcard에 저장하는 방법

public class DownloadimageActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    } 
    public void downloadPicture(View view) { 
    final ProgressDialog dialog = ProgressDialog.show(this, "Snapshot", 
      "retriving image.."); 
    dialog.show(); 
    new Thread(new Runnable() { 
     @Override 
     public void run() { 
      try { 
       final Bitmap downloadBitma = downloadBitmap("http://42.60.144.184:8081/snapshot.cgi?&user=admin&pwd=admin&resolution=8"); 
       final ImageView imageView = (ImageView) findViewById(R.id.imageView1); 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         imageView.setImageBitmap(downloadBitma); 

        } 
       }); 

      } catch (IOException e) { 
       e.printStackTrace(); 
      } finally { 
       dialog.dismiss(); 
      } 
     } 
    }).start(); 

} 

private Bitmap downloadBitmap(String url) throws IOException { 
    HttpUriRequest request = new HttpGet(url.toString()); 
    HttpClient httpClient = new DefaultHttpClient(); 
    HttpResponse response = httpClient.execute(request); 

    StatusLine statusLine = response.getStatusLine(); 
    int statusCode = statusLine.getStatusCode(); 
    if (statusCode == 200) { 
     HttpEntity entity = response.getEntity(); 
     byte[] bytes = EntityUtils.toByteArray(entity); 

     Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, 
       bytes.length); 

     return bitmap; 
    } else { 
     throw new IOException("Retrive failed, HTTP response code " 
       + statusCode + " - " + statusLine.getReasonPhrase()); 
    } 

} 
} 

나는 다음 코드로 시도했지만 작동하지 않습니다.

 ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    downloadBitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes); 
    //you can create a new file name "test.jpg" in sdcard folder. 
    File f = new File(Environment.getExternalStorageDirectory() 
          + File.separator + "test.jpg"); 
    f.createNewFile(); 
    //write the bytes in file 
    FileOutputStream fo = new FileOutputStream(f); 
    fo.write(bytes.toByteArray()); 
+1

"작동하지 않는다"는 것은 무엇을 의미합니까? 무엇이 실패합니까? 예외가 발생합니까? 또한 외부 저장소에 쓰기 권한을 요청 했습니까? – kabuko

+0

이클립스에서 새로운 변수를 생성하라는 메시지가 표시된다. downloadBitmap – radish

+0

Eclipse가 위의 코드에서 특정 라인을 요구 하는가? –

답변

1
final Bitmap downloadBitma = downloadBitmap(...); 

비트 맵 이름은 그래서이 작동하지 않습니다 끝에 '페이지를'없는 ...

downloadBitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes); 

아, 그리고 변수에 동일한 이름을 부여하지 않습니다 방법으로 - 나는 Java에서 합법적인지 여부조차 모른다.

+0

에 대한 마지막 의견을 읽었습니다. 처음에는 이것을 생각했지만 나중에 코드에서'downloadBitma'를 사용했습니다. –

+0

downloadBitma는 변수이며 코드가 제대로 작동하며 이미지 뷰에서 이미지를 다운로드하고 표시 할 수 있습니다. 이제 이미지 뷰에 표시되는 이미지를 저장하고 싶습니다. – radish

+0

'downloadBitma.compress (...)'로 압축하는 선을 변경하십시오. 그 점을 대답에 표시하려고합니다. 귀하의 회신에 감사드립니다 – Squonk

관련 문제