외부 이미지 URL이있는 setImageBitmap
을 사용하여 ImageView를 설정하고 있습니다. 인터넷에 연결되어 있지 않아도 나중에 사용할 수 있도록 이미지를 저장하고 싶습니다. 어디에서 어떻게 저장할 수 있습니까?URL에서 이미지를 저장하려면 어떻게해야합니까?
1
A
답변
3
SD 카드 또는 패키지 데이터로 저장해야합니다. 런타임에는 사용자가 액세스 할 수있는 권한 만 있기 때문입니다. 이 코드는 이미지 URL에서 비트 맵을 생성에 도움이 될 것입니다 How do I transfer an image from its URL to the SD card?
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();
}
}
관련 문제
- 1. URL의 이미지를 저장하려면 어떻게해야합니까?
- 2. 이미지를 내 폴더에 저장하려면 어떻게해야합니까?
- 3. 이미지를 BMP 형식으로 저장하려면 어떻게해야합니까?
- 4. 웹 이미지를 App Engine의 blobstore에 저장하려면 어떻게해야합니까?
- 5. websql (SQLite)에 이미지를 저장하려면 어떻게해야합니까?
- 6. cakephp에 HTML5 캔버스 이미지를 저장하려면 어떻게해야합니까?
- 7. ImageMagick을 사용하여 이미지를 열고 다시 저장하려면 어떻게해야합니까?
- 8. 웹 페이지의 이미지를 서버에 저장하려면 어떻게해야합니까?
- 9. JavaScript를 사용하여 오프라인에서 사용할 이미지를 저장하려면 어떻게해야합니까?
- 10. GDI +를 사용하지 않고 이미지를 저장하려면 어떻게해야합니까?
- 11. MonoTouch 사용 url에서 PDF 파일을 iBooks에 직접 저장하려면 어떻게해야합니까?
- 12. ImageView를 이미지로 저장하려면 어떻게해야합니까?
- 13. CPImage를 사용하여 URL에서 이미지를 다운로드하려면 어떻게해야합니까?
- 14. 웹에서 이미지를 저장하려면 UITableView
- 15. 개체 배열을 저장하려면 어떻게해야합니까?
- 16. MongoDB에 파일을 저장하려면 어떻게해야합니까?
- 17. 모양에 변수를 저장하려면 어떻게해야합니까?
- 18. UIImage를 BMP로 저장하려면 어떻게해야합니까?
- 19. iPhone에 plist를 저장하려면 어떻게해야합니까?
- 20. MKnapot에 MKAnnotation을 저장하려면 어떻게해야합니까?
- 21. UIButton을 배열에 저장하려면 어떻게해야합니까?
- 22. 결과를 영구 저장하려면 어떻게해야합니까?
- 23. JPA에 UUID를 저장하려면 어떻게해야합니까?
- 24. nsuserdefaults에 nsoperation을 저장하려면 어떻게해야합니까?
- 25. 양식 설정을 저장하려면 어떻게해야합니까?
- 26. UIImage를 파일로 저장하려면 어떻게해야합니까?
- 27. Subversion에서 커밋을 저장하려면 어떻게해야합니까?
- 28. 장고에 이미지/파일을 저장하려면 어떻게해야합니까?
- 29. 내 응용 프로그램 갤러리에 내장 카메라 이미지를 저장하려면 어떻게해야합니까?
- 30. 내 앱의 이메일에 형식을 지정하고 이미지를 저장하려면 어떻게해야합니까?
일일 도움이 될 수 있음. 'Environment.getExternalStorageDirectory()'는'String'을 리턴하지 않습니다. –
가 업데이트되었습니다. 감사합니다 –
무엇입니까 aReasonableSize –