Android调用系统Camera录像时不能指定保存路径的问题

官方文档中提到使用Video capture intent进行录像,可以使用intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri)指定录像文件保存路径。
但是实际使用中如果指定路径,不能获取video,非常奇怪(使用2.3.3)。
国外开发者也有类似问题,并提出了2个解决方案:
上述2种做法有点绕,以下是我的解决方法,先从onActivityResult中的intent得到文件路径,然后复制到指定目录,再删除原文件

 

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         super .onActivityResult(requestCode, resultCode, data); 
 
         if (resultCode == Activity.RESULT_OK) {
             //将存储在默认目录下的文件拷贝到指定文件夹下,删除默认文件
             String str = null ;
             try {          
                 if (requestCode == 1) {
                     Uri uri = Uri.parse(data.getData().toString());
                     ContentResolver cr = this .getContentResolver();
                     Cursor cursor = cr.query(uri, null , null , null , null );
                     cursor.moveToFirst();
                     str = cursor.getString(1);
                     videofileName = cursor.getString(2);
                     cursor.close();           
                 }
                 File destfile = new File(strsavedir + videofileName);
                 File srcfile = new File(str);
                 moveFileTo(srcfile, destfile);
                 super .onActivityResult(requestCode, resultCode, data);
             } catch (Exception e) {;           
             }
                     }
     }<div><h3> </h3><pre class= "brush:js;java;auto-link:false;" >public boolean moveFileTo(File srcFile, File destFile) throws IOException { 
         boolean iscopy = copyFileTo(srcFile, destFile); 
         if (!iscopy) 
             return false
         delFile(srcFile); 
         return true
     }</pre><pre class= "brush:js;java;auto-link:false;" >public boolean copyFileTo(File srcFile, File destFile) throws IOException { 
         if (srcFile.isDirectory() || destFile.isDirectory()) 
             return false ; // 判断是否是文件  
         FileInputStream fis = new FileInputStream(srcFile); 
         FileOutputStream fos = new FileOutputStream(destFile); 
         int readLen = 0; 
         byte[] buf = new byte[1024]; 
         while ((readLen = fis.read(buf)) != -1) { 
             fos.write(buf, 0, readLen); 
        
         fos.flush(); 
         fos.close(); 
         fis.close(); 
         return true
    
     public boolean delFile(File file) { 
         if (file.isDirectory()) 
             return false
         return file. delete (); 
     }</pre></div>
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值