我试图用我的应用程序创建的视频中创建一个库。 当我点击其中一个缩略图图片时,我想在我的偏好设置中保存所选的视频。
为了获得缩略图,我写了以下代码:
public void addVidsToArray() {
String[] projection = { MediaStore.Video.Media._ID };
String selection = MEDIA_DATA + " like ? ";
String [] selectionArgs = new String[] {"%Bla%"};
int columIndex;
int id;
Cursor videoCursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
selectionArgs,
null);
videoCursor.moveToFirst();
if(videoCursor.getCount() >= 1)
{
while (videoCursor.moveToNext()) {
columIndex = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID) ;
id = videoCursor.getInt(columIndex);
bitmaps.add(getThumbnail(id));
}
}
}
public Bitmap getThumbnail(int id)
{
ContentResolver crThumb = getContentResolver();
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap curThumb = MediaStore.Video.Thumbnails.getThumbnail(crThumb, id, MediaStore.Video.Thumbnails.MICRO_KIND, options);
curThumb = Bitmap.createScaledBitmap(curThumb, 300, 300, false);
return curThumb;
}
由于Uri身份,我得到了Tumbnail Pics。 现在我的问题是如何将其恢复到新文件中的正常路径以保存它。
以下代码是onClickListener,它应该将视频保存到首选项。 但是如果我将Uri添加到新文件中,那么文件就不会存在。
我不明白。
ga.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id)
{
String[] projection = { MediaStore.Video.Media._ID };
String selection = MEDIA_DATA + " like ? ";
String [] selectionArgs = new String[] {"%Bla%"};
int columIndex;
int id1;
Cursor videoCursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
projection,
selection,
selectionArgs,
null);
videoCursor.moveToFirst();
if(videoCursor.getCount() >= 1)
{
while (videoCursor.moveToNext()) {
columIndex = videoCursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID) ;
id1 = videoCursor.getInt(columIndex);
daten.add(id1+"");
}
}
Log.v("vid id", ""+daten.get(position));
int idData = Integer.parseInt(daten.get(position));
saveVideo(idData);
}
});
}
public void saveVideo(int idData)
{
String uriString = "content://media/external/video/media/" + idData;
Uri uri = Uri.parse(uriString);
File mVideoFile = new File(uriString);
Log.v("testpfad", ""+ mVideoFile.getAbsolutePath() + " " + mVideoFile.getParentFile().getAbsolutePath() + " " + mVideoFile.exists());
mVideoFileLen = getDurationFromURI(uri);
//setPreferences(mVideoFile.getAbsolutePath(), PICKED_VIDEO);
//setPreferences(mVideoFileLen, VIDEO_LEN);
//startActivity(new Intent(this, Finished.class));
}
最后,我需要在Uri的Id的帮助下创建一个文件。