將圖片保存到系統相冊中的方法(部分代碼未測試)


在實際使用過程中我們可能遇到將圖片保存到系統相冊的需求,如果做相機軟件更是如此。今天看到別人的博客中有些到這個功能,就研究了一下。發現我們可以通過Media.insertImage()方法來進行,而且還可以將其他文件夾中的圖片添加到系統相冊中。但在研究中發現了個問題,很多人說添加后發現系統相冊里一下子找不到,需要一會或者重啟才能發現添加的圖片,因此有了通知系統相冊更新的方法。我的手機版本是4.4,使用添加的方法后立刻可以在手機相冊的,相機文件夾中找到。這個……留着以后用到的話再詳細測試吧。

先說下使用方式:

1.將bitmap保存到系統相冊

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
        
        /**
         * 調用以上系統自帶的方法會把bitmap對象保存到系統圖庫中,
         * 但是這種方法無法指定保存的路徑和名稱
         * 方法的title、description參數只是插入數據庫中的字段,真實的圖片名稱系統會自動分配。
         */
        Media.insertImage(getContentResolver(), bitmap, "title", "描述文字");

 

這個title會在相冊中圖片的詳細屬性中看到,描述嘛可以寫null,沒啥意義。其實到最后系統會自己分配圖片的名字的。

 

二、將別的文件夾中的圖片插入到系統圖庫中

這里又涉及了通知系統圖庫更新的操作,這個手頭沒機子測試。所以暫時放在這

    /**
     * 可以將保存在別的文件夾中的圖片插入到系統圖庫中
     * @param context
     * @param bmp
     */
    public static void saveImageToGallery(Context context, Bitmap bmp) {
        // 首先保存圖片
        //圖片的根目錄
        File appDir = new File(Environment.getExternalStorageDirectory(), "Boohee");
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        //圖片名
        String fileName = System.currentTimeMillis() + ".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();
        }
        
        // 其次把文件插入到系統圖庫
        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://" + "Boohee")));
    }

 

三、網上說的通知系統圖庫更新方法

    /**
     * 通知系統相冊更新的方法01
     */
    private void notif01() {
        //imageFile是圖片的File對象;
        File imageFile = new File("/sdcard/Boohee/image.jpg");
        Uri localUri = Uri.fromFile(imageFile);
        Intent localIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, localUri); 
        sendBroadcast(localIntent);
    }
    
    /**
     * 通知系統相冊更新的方法02
     */
    private void notif02() {
        File imageFile = new File("/sdcard/Boohee/image.jpg");
        ContentValues localContentValues = new ContentValues();
        localContentValues.put("_data", imageFile.toString());
        localContentValues.put("description", "描述文字");
        localContentValues.put("mime_type", "image/jpeg");
        ContentResolver localContentResolver = getContentResolver();
        Uri localUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        localContentResolver.insert(localUri, localContentValues);
    }

 

系統源碼中保存圖片的代碼片段

Context context = params[0].context;
        Bitmap image = params[0].image;
        Resources r = context.getResources();

        try {
            // Create screenshot directory if it doesn't exist
            mScreenshotDir.mkdirs();

            // media provider uses seconds for DATE_MODIFIED and DATE_ADDED, but milliseconds
            // for DATE_TAKEN
            long dateSeconds = mImageTime / 1000;

            // Save the screenshot to the MediaStore
            ContentValues values = new ContentValues();
            ContentResolver resolver = context.getContentResolver();
            values.put(MediaStore.Images.ImageColumns.DATA, mImageFilePath);
            values.put(MediaStore.Images.ImageColumns.TITLE, mImageFileName);
            values.put(MediaStore.Images.ImageColumns.DISPLAY_NAME, mImageFileName);
            values.put(MediaStore.Images.ImageColumns.DATE_TAKEN, mImageTime);
            values.put(MediaStore.Images.ImageColumns.DATE_ADDED, dateSeconds);
            values.put(MediaStore.Images.ImageColumns.DATE_MODIFIED, dateSeconds);
            values.put(MediaStore.Images.ImageColumns.MIME_TYPE, "image/png");
            values.put(MediaStore.Images.ImageColumns.WIDTH, mImageWidth);
            values.put(MediaStore.Images.ImageColumns.HEIGHT, mImageHeight);
            Uri uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

            OutputStream out = resolver.openOutputStream(uri);
            image.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();

            // update file size in the database
            values.clear();
            values.put(MediaStore.Images.ImageColumns.SIZE, new File(mImageFilePath).length());
            resolver.update(uri, values, null, null);

           
        } catch (Exception e) {
           
        }

 

參考自:

http://stormzhang.github.io/android/2014/07/24/android-save-image-to-gallery/

http://blog.csdn.net/xu_fu/article/details/39158747

http://www.open-open.com/lib/view/open1414226791465.html

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM