MediaStore扫描 & 向MediaStore中插入文件记录

通知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);

 

 
posted @ 2012-10-24 18:04  wsjisji  阅读(5662)  评论(0编辑  收藏  举报