2017-06-26 64 views
2

我試圖下載功能添加到我的應用程序使用滑翔庫從他們網址立即下載圖片「米這是我保存的代碼圖片中的設備存儲,但都不盡如人意 正常工作與的Android 5.0.16.0.1但在另一個Android版本不順利將照片保存在設備[滑翔庫]

Toast.makeText(Pop.this, "Please wait ...", Toast.LENGTH_LONG).show(); 
        new AsyncTask<FutureTarget<Bitmap>, Void, Bitmap>() { 
         @Override protected Bitmap doInBackground(FutureTarget<Bitmap>... params) { 
          try { 
           return params[0].get(); 
           // get needs to be called on background thread 
          } catch (Exception ex) { 
           return null; 
          } 
         } 
         @Override protected void onPostExecute(Bitmap bitmap) { 
          if(bitmap == null) return; 
          MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "Art_"+System.currentTimeMillis(), "Beschrijving"); 
         } 
        }.execute(Glide.with(getApplicationContext()).load(url).asBitmap().diskCacheStrategy(DiskCacheStrategy.SOURCE).into(width, height)); 

請幫我

回答

6

1.Permissions清單文件

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

2.Glide下載圖片作爲位圖

Glide.with(mContext) 
     .load("YOUR_URL") 
     .asBitmap() 
     .into(new SimpleTarget<Bitmap>(100,100) { 
      @Override 
      public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) { 
        saveImage(resource); 
      } 
     }); 

3.保存位圖到你的記憶

private String saveImage(Bitmap image) { 
    String savedImagePath = null; 

    String imageFileName = "JPEG_" + "FILE_NAME" + ".jpg"; 
    File storageDir = new File(   Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) 
         + "/YOUR_FOLDER_NAME"); 
    boolean success = true; 
    if (!storageDir.exists()) { 
    success = storageDir.mkdirs(); 
    } 
    if (success) { 
     File imageFile = new File(storageDir, imageFileName); 
     savedImagePath = imageFile.getAbsolutePath(); 
     try { 
      OutputStream fOut = new FileOutputStream(imageFile); 
      image.compress(Bitmap.CompressFormat.JPEG, 100, fOut); 
      fOut.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     // Add the image to the system gallery 
     galleryAddPic(savedImagePath); 
     Toast.makeText(mContext, "IMAGE SAVED", Toast.LENGTH_LONG).show(); 
    } 
    return savedImagePath; 
} 

private void galleryAddPic(String imagePath) { 
    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
    File f = new File(imagePath); 
    Uri contentUri = Uri.fromFile(f); 
    mediaScanIntent.setData(contentUri); 
    sendBroadcast(mediaScanIntent); 
} 
+0

感謝這麼muuuuuuuuuuuuuuuch – pic

+0

非常感謝,但是當圖像保存質量差時 – pic

+0

默認值位圖格式由Glide是RGB_565。看看這個[link](https://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en) – anandwana001