주어진 URL에서 이미지를 다운로드하고 싶습니다. 다운로드 한 이미지는 SD 카드에 저장해야합니다. 아래 코드를 사용했습니다.URL에서 이미지 다운로드 및 저장
URL newurl = null;
try {
newurl = new URL(strHitRes);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection connection = (HttpURLConnection) newurl.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists()) file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
Toast.makeText(getApplicationContext(),"download successful",Toast.LENGTH_LONG).show();
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
이미지가 다운로드되지 않습니다. 심지어 디버그 모드에서 테스트 한 결과 비트 맵이 null 인 것으로 나타났습니다. 이 문제를 해결하는 방법.
에서의 허가 아래 사용하는 것을 잊지 마세요? – Jas
내 장치에서 정상적으로 작동합니다. 먼저 인터넷과 WRITE_EXTERNAL_STORAGE를 추가했는지 확인하십시오. 둘째, 유효한 이미지에 대한 링크가 있는지 확인하십시오. – anhtuannd
제가 필요한 모든 권한을 추가 –