android - 通过 Intent 录制视频,录制视频的大小为0

标签 android video android-intent

我使用 Intent 开发了视频录制应用程序。 它创建文件并存储在指定位置,但在 sdcard 上显示 0 字节。 会有什么问题? 我正在设置将视频存储在 sdcard 上不同位置的路径

谢谢。

public class RecordVideoActivity extends Activity{
    Button btn_NewVideoRec;
    Button btn_RecVideoList;
    ListView lstvwVideo;
    VideoView videoview;



    final static int REQUEST_VIDEO_CAPTURED = 1;
    Uri uriVideo = null; 
    Uri custLoc;
    File myRecFile;

    String pathtovideo;
    ArrayAdapter<String> videoadap;
    ArrayList<String> videolist;

    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.video_record_activity);

        btn_NewVideoRec = (Button)findViewById(R.id.btn_RecordNewVideo);
        btn_RecVideoList = (Button)findViewById(R.id.btn_RecordedVideoList);
        lstvwVideo = (ListView)findViewById(R.id.videolistview);
        videoview = (VideoView)findViewById(R.id.videoVw);
        btn_NewVideoRec.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
                Uri custLoc = getOutputVideoFileUri();
                //intent.putExtra(MediaStore.EXTRA_OUTPUT, custLoc);
                startActivityForResult(intent, REQUEST_VIDEO_CAPTURED);
            }
        });


        btn_RecVideoList.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                try{
                    System.out.println("path to video::"+pathtovideo);  
                    int lstindxofslash = pathtovideo.lastIndexOf("/");

                    System.out.println("last index of slash::"+lstindxofslash);
                    String pathofvdofldr = pathtovideo.substring(0,lstindxofslash);
                    System.out.println("path of video folder:"+pathofvdofldr);
                    File srcDir = new File(pathofvdofldr);
                    File dstDir = new File(Environment.getExternalStorageDirectory()+"/app");

                }catch (NullPointerException e) {
                    // TODO: handle exception
                    e.getStackTrace();
                }


                ListView videolw = (ListView)findViewById(R.id.videolistview);
                videolw.setVisibility(View.VISIBLE);

                videolist = getRecVideoFiles(Environment.getExternalStorageDirectory()+"/app/video");
                //videolist = getRecVideoFiles(Environment.getExternalStorageDirectory()+"/DCIM/Camera");
                //"Whatsapp/Media/WhatsApp Video/");
                videoadap = new ArrayAdapter<String>(RecordVideoActivity.this, android.R.layout.simple_list_item_1, videolist);
                videolw.setAdapter(videoadap);

                videolw.setOnItemClickListener(new OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> arg0, View arg1,
                            int pos, long arg3) {
                        // TODO Auto-generated method stub
                        Toast.makeText(getApplicationContext(), videolist.get(pos), Toast.LENGTH_SHORT).show();

                        videoview.setVisibility(View.VISIBLE);
                        btn_NewVideoRec.setVisibility(View.GONE);
                        btn_RecVideoList.setVisibility(View.GONE);
                        lstvwVideo.setVisibility(View.GONE);

                        MediaController mediaController = new MediaController(RecordVideoActivity.this);
                        mediaController.setAnchorView(videoview);
                        // Set video link (mp4 format )
                        Uri video = Uri.parse(videolist.get(pos));
                        videoview.setMediaController(mediaController);
                        videoview.setVideoURI(video);
                        videoview.start();

                    }
                });
            }

        });

    }

    public Uri getOutputVideoFileUri(){

        return Uri.fromFile(getOutPutVideoFile());

    }
    public File getOutPutVideoFile(){

        String videofolder = "/app/video";
        File videofldr = new File(Environment.getExternalStorageDirectory().toString()+videofolder);
        if (videofldr.isDirectory()) {
            myRecFile = new File(videofldr.getAbsolutePath()+"/"+System.currentTimeMillis()+".mp4");
            //myRecFile = audiofldr.toString()+"/"+System.currentTimeMillis()+".3gp";
            System.out.println("Path for existing folder::"+myRecFile);
        } else {
            videofldr.mkdir();
            System.out.println("path for created directory::"+myRecFile.getAbsolutePath());

            myRecFile = new File(videofldr.getAbsolutePath()+"/"+System.currentTimeMillis()+".mp4");
        }

        return myRecFile;

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode == RESULT_OK){
            if(requestCode == REQUEST_VIDEO_CAPTURED){
                //pathtovideo = getpathofVideo(data.getData());
                //System.out.println("pathv::"+pathtovideo);
                uriVideo = data.getData();
                //uriVideo = (Uri) data.getExtras().get(custLoc.toString());
                Toast.makeText(RecordVideoActivity.this,uriVideo.getPath(),Toast.LENGTH_LONG).show();
                System.out.println("UriVideoPath::"+uriVideo.getPath());

            }
        }else if(resultCode == RESULT_CANCELED){
            uriVideo = null;
            Toast.makeText(RecordVideoActivity.this, "Cancelled!",Toast.LENGTH_LONG).show();
        }


    }

        }




    private String getpathofVideo(Uri uriVideo) {
        // TODO Auto-generated method stub
        String[] proj = { MediaStore.Video.Media.DATA };
        Cursor cursor = managedQuery(uriVideo, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

    private ArrayList<String> getRecVideoFiles(String videorecpath) {
        // TODO Auto-generated method stub
        ArrayList<String> videofiles = new ArrayList<String>();
        File file = new File(videorecpath);
        file.mkdir();

        File[] f = file.listFiles();
        if (f.length == 0) {
            Toast.makeText(getApplicationContext(), "No Files!", Toast.LENGTH_SHORT).show();
        }else{
            for (int i = 0; i < f.length; i++) {
                System.out.println("from file.."+f[i].toString());
                videofiles.add(f[i].getName());
                System.out.println("from arraylist.."+videofiles.get(i).toString());
            }
        }
        return videofiles;
    }
}

最佳答案

代替您的代码试试这个:首先在获取数据并将其写入特定位置后录制视频。

Intent photoPickerIntent= new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                  startActivityForResult(Intent.createChooser(photoPickerIntent,"Take Video"),TAKE_VIDEO);

startActivityForResult 将是:

try
{
            AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(data.getData(), "r");
            FileInputStream fis = videoAsset.createInputStream();
            File root=new File(Environment.getExternalStorageDirectory(),"Directory");

              if (!root.exists()) {
                  root.mkdirs();
              }
              File file;

               file = new File(root,"test.mp4" );

              Uri uri=Uri.fromFile(file);


            FileOutputStream fos = new FileOutputStream(file);

            byte[] buf = new byte[1024];
            int len;
            while ((len = fis.read(buf)) > 0) {
                fos.write(buf, 0, len);
            }       
            fis.close();
            fos.close();


          } 
        catch (Exception e) 
        {
           e.printStackTrace();
        }

关于android - 通过 Intent 录制视频,录制视频的大小为0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14335903/

相关文章:

android - 如何将信息包含到我的 android 应用程序编译后?

android - 应用程序可以检测到文件(图像)已从设备复制吗?

java - 如何将自定义类型的数组从一个 Activity 传递到另一 Activity ?

android - 提示用户对应用内的 Android 应用进行评分

java - Android 上的 Apache Commons IO

android - 如何使用 google android Exoplayer 为广告和视频使用单一播放器

linux - 在 linux 上加入 AVCHD .mts 文件

android - Cordova/Crosswalk <video> 元素只播放一次,然后黑色

android - Android 应用程序可以与另一个应用程序交互吗?

android - 返回到特定 Activity