Andorid保存圖片至相冊功能實現(xiàn)

Andorid保存圖片至相冊功能實現(xiàn)
網(wǎng)上能搜到解決方式普遍是方式一,但是實際測試發(fā)現(xiàn),方式一是有問題的。
實現(xiàn)方式一:

public static void saveImageToGallery(Context context, Bitmap bmp) {
    // 首先保存圖片
    File appDir = new File(Environment.getExternalStorageDirectory(), "images");
    if (!appDir.exists()) {
        appDir.mkdir();
    }
    String fileName = “myImage” + ".jpg";
    File file = new File(appDir, fileName);
    try {
        FileOutputStream fos = new FileOutputStream(file);
        bmp.compress(CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    // 其次把文件插入到系統(tǒng)圖庫
    try {
        MediaStore.Images.Media.insertImage(context.getContentResolver(),
                file.getAbsolutePath(), fileName, null);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    // 最后通知圖庫更新
    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + path)));
}

方式一存在的問題:
1、部分手機相冊需要等待幾分鐘才能看見圖片。如紅米NOTE3
2、圖庫中會有兩張圖片存在,一張是保存的“imgaes/myImgae.jpg”,另一張是插入系統(tǒng)圖庫insertImage()方法生成的圖片,通常是“系統(tǒng)時間.jpg?!笔紫仍谙鄡灾谐霈F(xiàn)的是后面這張圖片。過會兒第一張圖片也會顯示在相冊中。
3、insertImage()方法在紅米NOTE3手機上會拋出這個異常:
MediaStore: Failed to insert image java.io.FileNotFoundException: No such file or directory at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)
實現(xiàn)方式二:
測試可行。只保存了一張圖片,并且能在相冊中立即看到。

 public void saveImageToGallery(Bitmap bmp) {
        // 首先保存圖片
        File appDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = "myImage.jpg";
        final File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //通知圖庫更新
        if (file != null && file.length() > 0) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    MediaScannerConnection.scanFile(
                            mContext,
                            new String[]{file.getAbsolutePath()},
                            null,
                            new MediaScannerConnection.OnScanCompletedListener() {
                                @Override
                                public void onScanCompleted(String path, Uri uri) {
                                    handler.sendEmptyMessage(0);
                                }
                            });
                }
            }).run();
        }
    }

Handle定義

    private static Handler handler=new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(Message msg) {
            Toast.makeText(mContext,"保存成功!",Toast.LENGTH_SHORT).show();
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            mContext.startActivity(intent);
        }
    };

方式二與方式一的區(qū)別:
1、方式二將圖片直接保存在手機系統(tǒng)圖片路徑下
2、圖庫更新方法不同

備注:
方式一的更新圖庫方法也是對的,只是獲取uri的方法需要改成Uri.fromFile(file),否則,就會造成方式一中的第二個問題(需要過一會兒才能在相冊中看到圖片)。

 mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容