阅读更多

I use the MediaStore.ACTION_VIDEO_CAPTURE intent class to capture the video, the video stored in default location(gallery),i want to store the video in specific location with specific name.

I use MediaStore.EXTRA_MEDIA_TITLE and MediaStore.EXTRA_MEDIA_OUTPUT but I don`t get the video at correct location, at least I need the path of recorded video.

 方案一:

Trick is to insert media into database before recording:

String fileName ="captureTemp.mp4";   ContentValues values =newContentValues();   values.put(MediaStore.Video.Media.TITLE, fileName);   cameraVideoURI = getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);    Intent intent =newIntent(MediaStore.ACTION_VIDEO_CAPTURE);   intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraVideoURI);   intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,0); intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, MAXIMUM_VIDEO_SIZE);                startActivityForResult(intent, CAPTURE_VIDEO_INTENT); 

and then onActivityResult() use saved cameraVideoUri to reference recorded video:

       String[] projection ={MediaStore.Video.Media.DATA,MediaStore.Video.Media.SIZE  };         Cursor cursor = managedQuery(cameraVideoURI, projection,null,null,null);         int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);         int column_index_size = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.SIZE);         cursor.moveToFirst();         String recordedVideoFilePath = cursor.getString(column_index_data);        int recordedVideoFileSize = cursor.getInt(column_index_size); 

方案二:

The solution from Zelimir doesn't work in my case (the videos were at the right location but had a size of zero bytes). So i've found another solution:

protectedvoid onActivityResult(int requestCode,int resultCode,Intent intent){   super.onActivityResult(requestCode, resultCode, intent);    if(resultCode != RESULT_OK)return;    try{     AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(intent.getData(),"r");     FileInputStream fis = videoAsset.createInputStream();     File tmpFile =newFile(Environment.getExternalStorageDirectory(),"VideoFile.3gp");      FileOutputStream fos =newFileOutputStream(tmpFile);      byte[] buf =newbyte[1024];     int len;     while((len = fis.read(buf))>0){         fos.write(buf,0, len);     }            fis.close();     fos.close();   }catch(IOException io_e){     // TODO: handle error   } 

}

The MediaStore.EXTRA_OUTPUT and the Uri aren't necessary in this case.

更多相关文章

  1. Android(安卓)NavigationBar 代码分析记录(一)
  2. Android(安卓)Studio 使用中遇到的问题和解决方案
  3. Android(安卓)报错:AGPBI:MethodHandle.invoke are only supporte
  4. Unable to execute dex: Multiple dex files define解决方案
  5. 解决Error:Android(安卓)Dex: com.android.dex.DexIndexOverflow
  6. 本人写的Android上RSS阅读器简单介绍
  7. Android第一行代码(2版)——阅读笔记
  8. Android:关闭软键盘自动弹出的解决方案
  9. Eclipse中使用Ant打Android包报错及解决方案

随机推荐

  1. Android(安卓)SwipeRefreshLayout+Recycl
  2. android:WebView在没有网络情况下,点击当
  3. Android监听收到的短信
  4. 获取网络信息,ip,子网掩码,网关,dns
  5. Android中使用achartengine生成图表
  6. Android(安卓)实现RippleEffect水波纹效
  7. 天天记录 - Android抓包 - 抓取HTTP,TCP协
  8. 【Android(安卓)初学】4、RadioButton &
  9. Android(安卓)自定义加载框dialog
  10. Android(安卓)- 收藏集