开发者社区 问答 正文

Android 摄像如何不保存录制时间很短的视频。

遇到一个问题,项目要求视频拍摄的视频要求小于两秒的视频不能保存,该怎么实现?

展开
收起
爵霸 2016-03-05 14:05:05 2651 分享 版权
1 条回答
写回答
取消 提交回答
  • 你这个得用异步来做吧,我最近写了一个异步获取图片的Demo,主要部分是这么写的:

     public class MainActivity extends Activity implements OnClickListener {
    
        private ImageView iv;
        private Button downloadButton;
        private TextView tv_show;
        private RotatingDoughnut rotatingDoughnut;
        private static final String IMG_PATH = "http://img.my.csdn.net/uploads/201604/06/1459922240_8285.jpg";
        private static final int MSG_SUCCESS = 0;
        private static final int MSG_FAILURE = 1;
        private static final int MSG_START_LOADING = 3;
        private static final int MSG_FINISH_LOADING = 4;
        private DownloadThread downloadThread;
        private Bitmap bitmap = null;
    
        @Override 
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            iv = (ImageView) findViewById(R.id.iv);
            tv_show = (TextView) findViewById(R.id.tv_show);
            rotatingDoughnut = (RotatingDoughnut) findViewById(R.id.progressBar);
            downloadButton = (Button) findViewById(R.id.btn_download);
            downloadButton.setOnClickListener(this);
        } 
    
        private Handler mHandler = new Handler() {
            public void handleMessage(Message msg) { 
    
                switch (msg.what) { 
                    case MSG_SUCCESS: 
                        rotatingDoughnut.setVisibility(View.INVISIBLE); 
                        iv.setImageBitmap(bitmap); 
                        downloadButton.setText("Success"); 
                        downloadButton.setEnabled(false); 
                        break; 
                    case MSG_FAILURE: 
                        Toast.makeText(getApplicationContext(), "获取图片失败", Toast.LENGTH_SHORT).show(); 
                        downloadButton.setText("Failure"); 
                        downloadButton.setEnabled(false); 
                        break; 
                    case MSG_START_LOADING: 
                        rotatingDoughnut.doughnutRotating(); 
                    default: 
                        break; 
                } 
            } 
    
            ; 
    
        }; 
    
    
        private class DownloadThread extends Thread {
            HttpURLConnection coon;
            InputStream inputStream;
    
            @Override 
            public void run() { 
                try { 
                    URL url = new URL(IMG_PATH);
                    if (url != null) {
                        coon = (HttpURLConnection) url.openConnection();
                        coon.setConnectTimeout(2000);
                        coon.setDoInput(true);
                        coon.setRequestMethod("GET");
                        coon.connect();
                        if (coon.getResponseCode() == 200) {
                            inputStream = coon.getInputStream();
                            bitmap = BitmapFactory.decodeStream(inputStream);
                            mHandler.obtainMessage(MSG_SUCCESS).sendToTarget();
                        } else { 
                            mHandler.obtainMessage(MSG_FAILURE).sendToTarget();
                        } 
                    } 
    
                } catch (MalformedURLException e) {
                    // TODO Auto-generated catch block 
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block 
                    e.printStackTrace();
                } 
                super.run(); 
            } 
    
    
        } 
    
        @Override 
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_download:
                    iv.setImageBitmap(null);
                    downloadThread = new DownloadThread();
                    if (!isConnectingToInternet()) { 
                        Toast.makeText(getApplicationContext(), "未检测到网络", Toast.LENGTH_SHORT).show();
                    } else { 
                        tv_show.setVisibility(View.GONE);
                        rotatingDoughnut.setVisibility(View.VISIBLE);
                        mHandler.obtainMessage(MSG_START_LOADING).sendToTarget();
                        mHandler.postDelayed(new Runnable() {
                            @Override 
                            public void run() { 
                                downloadThread.start();
                            } 
                        }, 2500); 
                    } 
                    break; 
                default: 
                    break; 
            } 
    
        } 
    
        /** 
         * 检测网络状态 
         * 
         * @return 
         */ 
        public boolean isConnectingToInternet() { 
            ConnectivityManager connectivity = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            if (connectivity != null) {
                NetworkInfo[] info = connectivity.getAllNetworkInfo();
                if (info != null)
                    for (int i = 0; i < info.length; i++)
                        if (info[i].getState() == NetworkInfo.State.CONNECTED) {
                            return true; 
                        } 
    
            } 
            return false; 
        } 
    
    } 
    2019-07-17 18:53:25
    赞同 展开评论