Android MediaStore掃描 & 向MediaStore中插入檔案記錄

來源:互聯網
上載者:User

http://www.cnblogs.com/androidwsjisji/archive/2012/10/24/2737604.html

通知MediaStore掃描某個檔案:

private void scanSingleFile(String filePath) {        if (filePath == null) {            return;        }// broadcast to scan file        boolean bmnt = Environment.getExternalStorageDirectory().getPath()                .startsWith("/mnt");        bmnt = mDownloadDestination                .equalsIgnoreCase(UpDownloadUtils.DOWNLOAD_DEST_SDCARD) ? bmnt                : false;        File file = new File(bmnt ? ("/mnt" + filePath) : filePath);        MediaScannerConnection.scanFile(mContext,                new String[] { file.toString() }, null,                new MediaScannerConnection.OnScanCompletedListener() {                    @Override                    public void onScanCompleted(String path, Uri uri) {                                            ContentResolver cr = mContext.getContentResolver();                        long datemodified = 0;                        long dateadded = 0;                        Cursor cursor = cr.query(uri, null, null, null, null);                        if (cursor != null && cursor.moveToFirst()) {                            datemodified = cursor.getLong(cursor                                    .getColumnIndex(MediaStore.MediaColumns.DATE_MODIFIED));                            dateadded = cursor.getLong(cursor                                    .getColumnIndex(MediaStore.MediaColumns.DATE_ADDED));                            cursor.close();                        }                        ContentValues values = new ContentValues();                        if (datemodified > 0                                && String.valueOf(datemodified).length() > 10) {                            values.put(MediaStore.MediaColumns.DATE_MODIFIED,                                    datemodified / 1000);                        }                        if (dateadded > 0                                && String.valueOf(dateadded).length() > 13) {                            values.put(MediaStore.MediaColumns.DATE_ADDED,                                    dateadded / 1000);                        }                        if (values.size() > 0) {                            cr.update(uri, values, null, null);                        }                    }                });    }

向MediaStore中插入檔案記錄

ContentValues newValues = new ContentValues(6);            String title = FileName.substring(0, pointIndex);            newValues.put(MediaStore.Images.Media.TITLE,                    FileName.substring(0, pointIndex));            newValues.put(MediaStore.Images.Media.DISPLAY_NAME,                    sourceFile.getName());            newValues.put(MediaStore.Images.Media.DATA, sourceFile.getPath());            newValues.put(MediaStore.Images.Media.DATE_MODIFIED,                    System.currentTimeMillis() / 1000);            newValues.put(MediaStore.Images.Media.SIZE, sourceFile.length());            newValues.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");            uri = contentResolver.insert(                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, newValues);
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.