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

來源:互聯網
上載者:User

標籤:

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

MediaStore.Images.Media.insertImage

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

 

導致這個問題的原因查看代碼後知道在插入原圖的同時系統自動產生了一個縮圖並儲存再相應的檔案目錄下,代碼如下。

解決辦法是把紅色框框中的代碼注釋掉就好了。

整理後的代碼如下:

    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;    }

 

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.