3

我已经使用 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;
    }
}
4

1 回答 1

2

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

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();
        }
于 2013-01-15T11:28:56.343 回答