Android向系統相冊中插入圖片,相冊中會出現兩張 同樣的圖片(只是圖片大小不一致)

向系統相冊中插入圖片調用此方法時,相冊中會出現兩張同樣的圖片android

MediaStore.Images.Media.insertImage

一張圖片是原圖一張圖片是縮略圖。表現形式爲:android4.4.4系統中插入的縮略圖和原圖在sdcard根目錄下的DCIM文件夾這種,Android5.0以上的機型插入的縮略圖在sdcard根目錄下的Pictures文件夾下,原圖存放在DCIM文件夾下。ide

 

致使這個問題的緣由查看代碼後知道在插入原圖的同時系統自動生成了一個縮略圖並保存再相應的文件目錄下,代碼以下。this

解決辦法是把紅色框框中的代碼註釋掉就行了。url

整理後的代碼以下:spa

    public void insertImage(String fileName) {
        // Toast.makeText(this, "插入圖片", Toast.LENGTH_LONG).show();
        try {
            
            insertImageW(getContentResolver(), fileName, new File(fileName).getName(),
                    new File(fileName).getName());
            Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            Uri uri = Uri.fromFile(new File(fileName));
            intent.setData(uri);
            sendBroadcast(intent);
            MediaScannerConnection.scanFile(this, new String[] { fileName }, new String[] { "image/jpeg" },
                    new MediaScannerConnection.MediaScannerConnectionClient() {
                        @Override
                        public void onMediaScannerConnected() {

                        }

                        @Override
                        public void onScanCompleted(String path, Uri uri) {
                            photoGalleryFragment.addCaptureFile(path);
                        }
                    });
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
public String insertImageW(ContentResolver cr, String imagePath, String name, String description)
            throws FileNotFoundException {
        // Check if file exists with a FileInputStream
        FileInputStream stream = new FileInputStream(imagePath);
        try {
            Bitmap bm = BitmapFactory.decodeFile(imagePath);
            String ret = insertImageT(cr, bm, name, description);
            bm.recycle();
            return ret;
        } finally {
            try {
                stream.close();
            } catch (IOException e) {
            }
        }
    }

    public  String insertImageT(ContentResolver cr, Bitmap source, String title, String description) {
        ContentValues values = new ContentValues();
        values.put(Images.Media.TITLE, title);
        values.put(Images.Media.DESCRIPTION, description);
        values.put(Images.Media.MIME_TYPE, "image/jpeg");

        Uri url = null;
        String stringUrl = null; /* value to be returned */

        try {
            String CONTENT_AUTHORITY_SLASH = "content://" + "media" + "/";
            Uri uri = Uri.parse(CONTENT_AUTHORITY_SLASH + "external" + "/images/media");
            url = cr.insert(uri, values);
        } catch (Exception e) {
            Log.e("heheh", "Failed to insert image", e);
            if (url != null) {
                cr.delete(url, null, null);
                url = null;
            }
        }

        if (url != null) {
            stringUrl = url.toString();
        }

        return stringUrl;
    }
相關文章
相關標籤/搜索