ym——Android从零开始(25)(调用系统的相机、录音、上传文件)(新)

转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持!


调用系统的相机、录音、上传文件调用系统相机

相机

Intent intent = newIntent("android.media.action.IMAGE_CAPTURE");
startActivity(intent);

录音系统架构

圆圈里面是状态,双箭头是监听,单箭头是执行


录音

public class MainActivity extends Activity {
    private TextView tv;
    private MediaRecorder mr;
    private Handler mHandelr = new Handler();
    private MyTask m;
    private int time;
    private Button bt_stop;
    private Button bt_start;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView)findViewById(R.id.tv_time);
        bt_start = (Button)findViewById(R.id.bt_start);
        bt_stop = (Button)findViewById(R.id.bt_stop);
        mr = new MediaRecorder();
    }
    public void start(View v){
        try {
            // 重新设置
            mr.reset();
            // 指定声音来源-->>麦克风
            mr.setAudioSource(MediaRecorder.AudioSource.MIC);
            // 指定文件输出的格式
            mr.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            // 指定音频的编码格式
            mr.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            // 存放的文件
               File file = newFile(Environment.getExternalStorageDirectory(),System.currentTimeMillis()+".3gp");
            // 指定文件输出的路径
            mr.setOutputFile(file.getAbsolutePath());
            // 准备
            mr.prepare();
            // 开始刻录
            mr.start();
            m = new MyTask();
            mHandelr.post(m);
            bt_start.setText("录音中....");
            
            
        } catch (Exception e) {
            // TODO: handle exception
        }
    }
    private class MyTask implements Runnable{
        @Override
        public void run() {
            // TODO Auto-generated methodstub
            mHandelr.postDelayed(this, 1000);
            time++;
            tv.setText(time + "秒");
        }
        
    }
    public void stop(View v){
        mr.stop();
        mHandelr.removeCallbacks(m);
        bt_start.setText("录音");
    }
}

文件上传

导入第三方jar 


Android客户端

public void upload(View v) {
        try {
        HttpClient httpClient = new DefaultHttpClient();
        // 请求指定URL
        String uri = "http://10.0.2.2:8081/test_2_9_service/upload.action";
        HttpPost post = new HttpPost(uri);
        String fileName = tv_name.getText().toString();
        // 创建待处理的文件
        FileBody bin = new FileBody(new File(Environment.getExternalStorageDirectory(),fileName));
        // 创建待处理的表单域内容文本
        StringBody comment = new StringBody(fileName);
        // 对请求的表单域进行填充
        MultipartEntityreqEntity = new MultipartEntity();
        
        reqEntity.addPart("upload",bin);
        reqEntity.addPart("uploadFileName",comment);
        // 设置请求
        post.setEntity(reqEntity);
        // 执行
        HttpResponse response =httpClient.execute(post);
        int code = response.getStatusLine().getStatusCode();
        if (code == 200) {
            Toast.makeText(this, "O了", 200).show();
            
        }
        
        } catch (Exception e) {
            // TODO Auto-generated catchblock
            e.printStackTrace();
        }
        
    }


Web服务端

try {
             // 指定文件存放位置
            String path = ServletActionContext.getRequest().getRealPath("upload");
            System.out.print(path);
            // 文件存放目录
            File dir = new File(path);
            if(!dir.exists())
            {
                dir.mkdirs();
            }
            // 要保存文件
            File saveFile = new File(dir,uploadFileName);
            // struts2框架得到的文件流
            FileInputStream fis = new FileInputStream(upload);
            // 要输出的文件流
            FileOutputStream fos = newFileOutputStream(saveFile);
            
            byte[] buffer = new byte[1024];
            int len = 0;
            while ((len = fis.read(buffer))!= -1) {
                 fos.write(buffer, 0, len);
               
            }
            fos.close();
            fis.close();
            // 上传成功提示
            ServletActionContext.getResponse().getWriter().print("上传成功");

课后问题

激活系统已经存在的组件  

使用intent


怎么调用系统的相机去拍照

Intent intent = newIntent("android.media.action.IMAGE_CAPTURE");
startActivity(intent);
MediaRecoder如何使用
mediaRecorder.reset();//重新设置
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);////设置麦克风
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//设置输出格式
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//设置音频编码Encoder
mediaRecorder.setOutputFile(path);//设置音频文件保存路径
mediaRecorder.prepare();//准备录制
mediaRecorder.start();//开始录制
//停止录音和释放资源
mediaRecorder.stop();
mediaRecorder.release();


HttpClient4.0来实现文件的上传

HttpClient httpClient = newDefaultHttpClient();
              // 请求指定URL
              String uri ="http://10.0.2.2:8081/test_2_9_service/upload.action";
              HttpPost post = new HttpPost(uri);
              String fileName =tv_name.getText().toString();
              // 创建待处理的文件
              FileBody bin = new FileBody(newFile(Environment.getExternalStorageDirectory(),fileName));
              // 创建待处理的表单域内容文本
              StringBody comment = new StringBody(fileName);
              // 对请求的表单域进行填充
              MultipartEntity reqEntity = newMultipartEntity();
              
              reqEntity.addPart("upload",bin);
              reqEntity.addPart("uploadFileName",comment);
              // 设置请求
              post.setEntity(reqEntity);
              // 执行
              HttpResponse response = httpClient.execute(post);
              int code =response.getStatusLine().getStatusCode();
              if (code == 200) {
                     Toast.makeText(this,"O了",200).show();
                     
              }




你可能感兴趣的:(ym——Android从零开始(25)(调用系统的相机、录音、上传文件)(新))