欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android實現(xiàn)保存圖片到本地并在相冊中顯示

 更新時間:2017年03月27日 10:10:05   作者:我勒個茜  
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)保存圖片到本地并在相冊中顯示的相關(guān)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下

Android中拍照保存圖片到本地是常見的一種需求,之前碰到了一個問題,就是在4.4中,刷新相冊會出現(xiàn)ANR,經(jīng)過一番百度解決了這個問題。

首先是保存圖片到本地

private static final String SAVE_PIC_PATH = Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED) ? Environment.getExternalStorageDirectory().getAbsolutePath() :
      "/mnt/sdcard";//保存到SD卡
  private static final String SAVE_REAL_PATH = SAVE_PIC_PATH + "/good/savePic";

   //保存的確切位置,根據(jù)自己的具體需要來修改

public void saveFile(Bitmap bm, String fileName, String path) throws IOException {
    String subForder = SAVE_REAL_PATH + path;
    File foder = new File(subForder);
    if (!foder.exists()) {
      foder.mkdirs();
    }
    File myCaptureFile = new File(subForder, fileName);
    if (!myCaptureFile.exists()) {
      myCaptureFile.createNewFile();
    }
    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
    bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
    bos.flush();
    bos.close();
    Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT).show();

以上就是保存圖片的方法,保存完畢之后就是要通知相冊刷新了,
在4.4中:

MediaScannerConnection.scanFile(this, new String[]{SAVE_REAL_PATH+ "/" + fileName}, null, new MediaScannerConnection.OnScanCompletedListener() {
      @Override
      public void onScanCompleted(String path, Uri uri) {
        Log.e( "onScanCompleted: ", path);
        Log.e( "onScanCompleted: ", uri.toString());
      }
    });

在4.4以上的是發(fā)送廣播來實現(xiàn):

Intent intent = new Intent(Intent.ACTION_MEDIA_MOUNTED); //這是刷新SD卡
//    Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);  // 這是刷新單個文件
    Uri uri = Uri.fromFile(new File(SAVE_REAL_PATH));
    intent.setData(uri);
    sendBroadcast(intent);

以上兩種方式有所區(qū)別,刷新SD卡的uri和刷新單個文件的uri的path不同,刷新SD卡的path就是外部存儲的根目錄,刷新單個文件的path就是你保存圖片的具體路徑,這是暫時我所遇到的坑,4.4一下還沒測試,如果測試出現(xiàn)問題,歡迎評論。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論