URL에서 이미지를로드하고 Android의 디바이스 메모리에 저장하는 방법은 무엇입니까? Piscasso 또는 oser laibrary를 사용한다고 말하지 마십시오. 다음과 같이해야합니다 : 장치가 인터넷 연결을하면 URL에서 ImageView로 이미지를로드하고 장치의 메모리에 이미지를 저장합니다. 그렇지 않으면 imageView에 이미지를 저장해야합니다. 도움을 주셔서 감사합니다.URL에서 이미지를로드하고 android에 메모리에 저장하십시오.
P. 죄송합니다. 영어를 잘 모른다는 이유로 실수를 할 수 있습니다. 이 내 수업 : 당신은 그런 식으로, 별도의 스레드에서 사용한다 그리고 imageUrl
public Bitmap getBitmapFromURL(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection)
url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream inputStream = connection.getInputStream();
Bitmap imageBitmap = BitmapFactory.decodeStream(inputStream);
return imageBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
에서 로딩 이미지
가방법 :이 코드를 사용하는
public class ImageManager {
String file_path;
Bitmap bitmap = null;
public Bitmap donwoaledImageFromSD() {
File image = new File(Environment.getExternalStorageDirectory().getPath(),file_path);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
return bitmap;
}
private void savebitmap() {
File file = new File("first");
file_path = file.getAbsolutePath();
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90,fileOutputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
public void fetchImage(final String url, final ImageView iView) {
new AsyncTask<String, Void, Bitmap>() {
protected Bitmap doInBackground(String... iUrl) {
try {
InputStream in = new URL(url).openStream();
bitmap = BitmapFactory.decodeStream(in);
savebitmap();
} catch (Exception e) {
donwoaledImageFromSD();
}
return bitmap;
}
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
if (iView != null) {
iView.setImageBitmap(result);
}
}
}.execute(url);
}
}
Y는 유 글라이드와 같은 신뢰할 수있는 라이브러리를 사용하지 말아 : 당신은
getBitmapFromURL()
를 호출해야하므로업데이트 2
방법
getBitmapFromURL()
하지만ImageView
예를 들어,이 방법, UI 스레드에서 업데이트해야합니다 , Picaso 등 –스탠드 아트 라이브러리 (글라이드, 피카소 등)를 사용하지 않는 기준 중 하나입니다. –
시도한 내용과 발생한 문제를 보여주는 [mcve]를 제공해주십시오. – CommonsWare