Android mediastore Scan & Insert file records into mediastore

Source: Internet
Author: User

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

Notify mediastore to scan a file:

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

Insert file records into 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);
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.