android如何浏览并选择图片、音频 、视频

 <1> 选择按钮的代码
  // 选取图片按钮单击事件
 public void click_xuanqutupian(View source) {
  Intent intent = new Intent();
  /* 开启Pictures画面Type设定为image */
  intent.setType(“image/*”);
  //intent.setType(“audio/*”); //选择音频
  //intent.setType(“video/*”); //选择视频 (mp4 3gp 是android支持的视频格式)
  //intent.setType(“video/*;image/*”);//同时选择视频和图片  
  /* 使用Intent.ACTION_GET_CONTENT这个Action */
  intent.setAction(Intent.ACTION_GET_CONTENT);
  /* 取得相片后返回本画面 */
  startActivityForResult(intent, 1);
 }


<2> 取得选择的项 以后 处理的地方

@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  // 选取图片的返回值
  if (requestCode == 1) {
    if (resultCode == RESULT_OK) {
      Uri uri = data.getData();
      Cursor cursor = getContentResolver().query(uri, null, null,null, null);
      cursor.moveToFirst();
      // String imgNo = cursor.getString(0); // 图片编号
       imgPath = cursor.getString(1); // 图片文件路径
       String imgSize = cursor.getString(2); // 图片大小
       String imgName = cursor.getString(3); // 图片文件名
       fileName = imgName;
      fileSize = imgSize;
      // Log.e(“uri”, uri.toString());
      ContentResolver cr = this.getContentResolver();
      try {
        Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
        ImageView imageView = (ImageView) findViewById(R.id.imview);
        /* 将Bitmap设定到ImageView */
        imageView.setImageBitmap(bitmap);
      } catch (FileNotFoundException e) {
       // Log.e(“Exception”, e.getMessage(),e);
      }
   }
  }
  // 拍照的返回值
  if (requestCode == 2) {
   if (resultCode == RESULT_OK) {
    imgPath = data.getStringExtra(“filePath”);
    fileName = data.getStringExtra(“fileName”);
    fileSize = data.getStringExtra(“fileSize”);
    // 读取拍照所得的文件
    try {
     Bitmap bitmap = this.getLoacalBitmap(imgPath);
     ImageView imageView = (ImageView) findViewById(R.id.imview);
     imageView.setImageBitmap(bitmap);
    } catch (Exception e) {
     // TODO: handle exception
    }
    //
   }
  }
  super.onActivityResult(requestCode, resultCode, data);
 }

你可能感兴趣的:(Android开发(转载))