【问题标题】:How to store a JPG remote file into the phone internalMemory/sdcard?如何将 JPG 远程文件存储到手机内存/SD 卡中?
【发布时间】:2012-04-06 20:50:44
【问题描述】:

我需要在我的应用中添加新功能。我必须将大量来自互联网的 JPG 图片存储到手机中。

如果手机有sdcard,JPG文件必须存储在sdcard上。如果没有,它们必须存储在 internalMemory 中,前提是手机有空间执行此操作。

那么,我应该做这些事情:

  1. 检查手机是否有 SDCARD。如果有,SDCARD dir 就是命运,如果没有,INTERNAL MEMORY 就是命运。
  2. 检查手机是否有空间存储照片。
  3. 如果有空间,将照片存储到名为/Magazine/ 的目录中

我知道如何从互联网上获取 JPG 文件以及如何将其转换为 BITMAP,但这是我唯一知道的,我不知道如何将其存储为 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;
}

我在谷歌搜索如何做我需要的事情,但我找不到任何明确的信息,我更愿意向你询问最好的方法,最佳的方法。

谢谢

【问题讨论】:

    标签: android bitmap store sd-card jpeg


    【解决方案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()
    }
    

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      检查 SDCard 是否存在:

      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() 并与您的文件夹名称连接

      【讨论】:

        猜你喜欢
        • 2015-06-15
        • 2011-07-04
        • 1970-01-01
        • 2011-06-08
        • 1970-01-01
        • 1970-01-01
        • 2020-01-11
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多