2012-03-22 77 views
0

我需要在我的應用程序中添加一項新功能。我必須將大量來自互聯網的JPG圖像存儲到手機中。如何將JPG遠程文件存儲到手機的internalMemory/sdcard中?

如果手機有SD卡,JPG文件必須存儲在SD卡上。如果沒有,只有手機有足夠的空間才能存儲它們,它們必須存儲在內部存儲器中。

的話,我應該做這些事情:

  1. 檢查電話富人SD卡。如果有,SDCARD dir是命運,否則,INTERNAL MEMORY是命運。
  2. 檢查手機是否有空間存放照片。
  3. 如果有空間,將照片存儲到一個名爲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來問你要做到這一點的最好方式,最佳的方式。

感謝

回答

1

所以上述方法的返回值讓你從互聯網上的位圖。

使用下面的代碼將其保存爲JPG到SD卡。

try { 
    bm.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/sdcard/Magazine/Image001.jpg"))); 
    // bm is your decoded bitmap from internet 
} catch (FileNotFoundException e) {    
    e.printStackTrace() 
} 

我希望它可以幫助你。

0

要檢查是否SD卡存在:

if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) { 

Log.i("tag", "SDCard is mounted"); 

} 

要獲得大小放在手機內存:

Get free space on internal memory

File path = Environment.getDataDirectory(); 
StatFs stat = new StatFs(path.getPath()); 
long blockSize = stat.getBlockSize(); 
long availableBlocks = stat.getAvailableBlocks(); 
return Formatter.formatFileSize(this, availableBlocks * blockSize); 

添加烏爾文件夾名稱只得到context.getFilesDir()與烏爾串聯文件夾名稱

相關問題