android调用本地录制程序获取录制文件路径的问题(摘)

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值