zoukankan      html  css  js  c++  java
  • 【转】android 选取图片

    这几天 在学习并开发android系统的图片浏览 音频 视频 的浏览 由于是第一次做android系统(java也不会)
    遇到了很多问题 如何浏览并选择图片 音频 视频也花了我好几天的时间
    我把它整理处理 以便帮助和我一样的同学 也同时防备自己忘记
    <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
  • 相关阅读:
    poj 3616 Milking Time
    poj 3176 Cow Bowling
    poj 2229 Sumsets
    poj 2385 Apple Catching
    poj 3280 Cheapest Palindrome
    hdu 1530 Maximum Clique
    hdu 1102 Constructing Roads
    codeforces 592B The Monster and the Squirrel
    CDOJ 1221 Ancient Go
    hdu 1151 Air Raid(二分图最小路径覆盖)
  • 原文地址:https://www.cnblogs.com/Mr-Nobody/p/3540442.html
Copyright © 2011-2022 走看看