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

来源:互联网 发布:遗传算法的书pdf版本 编辑:程序博客网 时间:2024/06/05 16:53
官方文档中提到使用Video capture intent进行录像,可以使用intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri)指定录像文件保存路径。
但是实际使用中如果指定路径,不能获取video,非常奇怪(使用2.3.3)。
国外开发者也有类似问题,并提出了2个解决方案:
http://stackoverflow.com/questions/13035638/intent-for-video-recording-gives-error
http://stackoverflow.com/questions/9003423/error-calling-mediastore-action-video-capture
上述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 = newFile(strsavedir + videofileName);
                File srcfile = newFile(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) 
            returnfalse
        delFile(srcFile); 
        returntrue
    }</pre><pre class="brush:js;java;auto-link:false;">public boolean copyFileTo(File srcFile, File destFile) throws IOException { 
        if(srcFile.isDirectory() || destFile.isDirectory()) 
            returnfalse;// 判断是否是文件  
        FileInputStream fis = newFileInputStream(srcFile); 
        FileOutputStream fos = newFileOutputStream(destFile); 
        int readLen = 0; 
        byte[] buf = newbyte[1024]; 
        while((readLen = fis.read(buf)) != -1) { 
            fos.write(buf, 0, readLen); 
        
        fos.flush(); 
        fos.close(); 
        fis.close(); 
        returntrue
    
    public boolean delFile(File file) { 
        if(file.isDirectory()) 
            returnfalse
        returnfile.delete(); 
    }</pre></div>
原创粉丝点击