我需要在我的應用程序中添加一項新功能。我必須將大量來自互聯網的JPG圖像存儲到手機中。如何將JPG遠程文件存儲到手機的internalMemory/sdcard中?
如果手機有SD卡,JPG文件必須存儲在SD卡上。如果沒有,只有手機有足夠的空間才能存儲它們,它們必須存儲在內部存儲器中。
的話,我應該做這些事情:
- 檢查電話富人SD卡。如果有,SDCARD dir是命運,否則,INTERNAL MEMORY是命運。
- 檢查手機是否有空間存放照片。
- 如果有空間,將照片存儲到一個名爲DIR
/Magazine/
我知道如何從互聯網以及如何將其轉化成位圖得到一個JPG文件,但它是唯一想我知道,我不知道如何將它存儲爲JPG,我不知道其他的東西。
public static Bitmap getRemoteBitmap(String url) {
Bitmap bm=null;
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
try {
((AbstractHttpClient) httpclient).getCredentialsProvider().setCredentials(
new org.apache.http.auth.AuthScope(null,-1),
new org.apache.http.auth.UsernamePasswordCredentials(MagazineStatus._username, MagazineStatus._password));
response = httpclient.execute(new HttpGet(url));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK) {
try {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
bm=BitmapFactory.decodeStream(content);
}catch(Exception ex) {Log.e("DBF Error",ex.toString());}
}else {
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
}catch(ClientProtocolException cpe) {Log.e("ClientProtocolException @ at FPT",cpe.toString());} catch(Exception ex) {Log.e("Exception at FETCHPROJECTASK",ex.toString());}
return bm;
}
我在谷歌搜索怎麼做我所需要的東西,但我無法找到任何明確的信息,我prefeer來問你要做到這一點的最好方式,最佳的方式。
感謝