이미지 형식 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());
"작동하지 않는다"는 것은 무엇을 의미합니까? 무엇이 실패합니까? 예외가 발생합니까? 또한 외부 저장소에 쓰기 권한을 요청 했습니까? – kabuko
이클립스에서 새로운 변수를 생성하라는 메시지가 표시된다. downloadBitmap – radish
Eclipse가 위의 코드에서 특정 라인을 요구 하는가? –