/**需要依赖Okio*/
private static final String VIDEO_BASE_URI = "content://media/external/video/media";
/***
*
* @param videoPath
* @param context
*/
private void insertVideo(String videoPath, Context context) {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(videoPath);
int nVideoWidth = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
int nVideoHeight = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
int duration = Integer
.parseInt(retriever
.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
long dateTaken = System.currentTimeMillis();
File file = new File(videoPath);
String title = file.getName();
String filename = file.getName();
String mime = "video/mp4";
ContentValues mCurrentVideoValues = new ContentValues(9);
mCurrentVideoValues.put(MediaStore.Video.Media.TITLE, title);
mCurrentVideoValues.put(MediaStore.Video.Media.DISPLAY_NAME, filename);
mCurrentVideoValues.put(MediaStore.Video.Media.DATE_TAKEN, dateTaken);
mCurrentVideoValues.put(MediaStore.MediaColumns.DATE_MODIFIED, dateTaken / 1000);
mCurrentVideoValues.put(MediaStore.Video.Media.MIME_TYPE, mime);
mCurrentVideoValues.put(MediaStore.Video.Media.DATA, videoPath);
mCurrentVideoValues.put(MediaStore.Video.Media.WIDTH, nVideoWidth);
mCurrentVideoValues.put(MediaStore.Video.Media.HEIGHT, nVideoHeight);
mCurrentVideoValues.put(MediaStore.Video.Media.RESOLUTION, Integer.toString(nVideoWidth) + "x" + Integer.toString(nVideoHeight));
mCurrentVideoValues.put(MediaStore.Video.Media.SIZE, new File(videoPath).length());
mCurrentVideoValues.put(MediaStore.Video.Media.DURATION, duration);
ContentResolver contentResolver = context.getContentResolver();
Uri videoTable = Uri.parse(VIDEO_BASE_URI);
Uri uri = contentResolver.insert(videoTable, mCurrentVideoValues);
writeFile(videoPath, mCurrentVideoValues, contentResolver, uri);
}
保存图片
/**需要依赖Okio*/
/***
*
* @param imagePath
* @param context
*/
private void insertImage(String imagePath, Context context) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DISPLAY_NAME, "IMG1024.JPG");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
ContentResolver contentResolver = context.getContentResolver();
Uri collection = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
Uri item = contentResolver.insert(collection, values);
writeFile(imagePath, values, contentResolver, item);
contentResolver.update(item, values, null, null);
}
写文件方法:
/***
*
* @param videoPath
* @param context
*/
private void insertVideo(String videoPath, Context context) {
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(videoPath);
int nVideoWidth = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
int nVideoHeight = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
int duration = Integer
.parseInt(retriever
.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
long dateTaken = System.currentTimeMillis();
File file = new File(videoPath);
String title = file.getName();
String filename = file.getName();
String mime = "video/mp4";
ContentValues mCurrentVideoValues = new ContentValues(9);
mCurrentVideoValues.put(MediaStore.Video.Media.TITLE, title);
mCurrentVideoValues.put(MediaStore.Video.Media.DISPLAY_NAME, filename);
mCurrentVideoValues.put(MediaStore.Video.Media.DATE_TAKEN, dateTaken);
mCurrentVideoValues.put(MediaStore.MediaColumns.DATE_MODIFIED, dateTaken / 1000);
mCurrentVideoValues.put(MediaStore.Video.Media.MIME_TYPE, mime);
mCurrentVideoValues.put(MediaStore.Video.Media.DATA, videoPath);
mCurrentVideoValues.put(MediaStore.Video.Media.WIDTH, nVideoWidth);
mCurrentVideoValues.put(MediaStore.Video.Media.HEIGHT, nVideoHeight);
mCurrentVideoValues.put(MediaStore.Video.Media.RESOLUTION, Integer.toString(nVideoWidth) + "x" + Integer.toString(nVideoHeight));
mCurrentVideoValues.put(MediaStore.Video.Media.SIZE, new File(videoPath).length());
mCurrentVideoValues.put(MediaStore.Video.Media.DURATION, duration);
ContentResolver contentResolver = context.getContentResolver();
Uri videoTable = Uri.parse(VIDEO_BASE_URI);
Uri uri = contentResolver.insert(videoTable, mCurrentVideoValues);
writeFile(videoPath, mCurrentVideoValues, contentResolver, uri);
}
private void writeFile(String imagePath, ContentValues values, ContentResolver contentResolver, Uri item) {
try (OutputStream rw = contentResolver.openOutputStream(item, "rw")) {
// Write data into the pending image.
Sink sink = Okio.sink(rw);
BufferedSource buffer = Okio.buffer(Okio.source(new File(imagePath)));
buffer.readAll(sink);
values.put(MediaStore.Video.Media.IS_PENDING, 0);
contentResolver.update(item, values, null, null);
new File(imagePath).delete();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
Cursor query = getContentResolver().query(item, null, null, null);
if (query != null) {
int count = query.getCount();
Log.e("writeFile","writeFile result :" + count);
query.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
网友评论