• Android从Camera中获取图片的两种方法


    方法一:

    此方法会由Camera直接产生照片回传给应用程序,但是返回的是压缩图片,显示不清晰

    1
    2
    3
    4
    5
    6
    try {
         Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
         startActivityForResult(cameraIntent, CAMERA_WITH_DATA);
     } catch (ActivityNotFoundException e) {
         e.printStackTrace();
     }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    Bundle bundle = data.getExtras();
    bmp_selectedPhoto = (Bitmap) bundle.get("data");
    if (bmp_selectedPhoto != null)
        bmp_selectedPhoto.recycle();
    bmp_selectedPhoto = (Bitmap) data.getExtras().get("data");
    // int scale = ImageThumbnail.reckonThumbnail(bitMap.getWidth(),
    // bitMap.getHeight(), 500, 600);
    // bitMap = ImageThumbnail.PicZoom(bitMap,
    // (int) (bitMap.getWidth() / scale),
    // (int) (bitMap.getHeight() / scale));
    // bitMap = ThumbnailUtils.extractThumbnail(bitMap, 200, 200);
    if(bmp_selectedPhoto != null){
        home_view.setBackground(new BitmapDrawable(getResources(),
        bmp_selectedPhoto));
    }

    方法二:

    此方法所拍即所得,但是会在Sd卡上产生临时文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    Intent cameraIntent = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
     
            File appDir = new File(Environment.getExternalStorageDirectory()
                    + "/KengDieA");
             
            if (!appDir.exists()) {
                appDir.mkdir();
            }
     
            mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory()
                    + "/KengDieA/", "kengDiePic"
                    + String.valueOf(System.currentTimeMillis()) + ".jpg"));
            cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);
     
            try {
                cameraIntent.putExtra("return-data", true);
                startActivityForResult(cameraIntent, CAMERA_WITH_DATA);
            } catch (Exception e) {
                e.printStackTrace();
            }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    ContentResolver cr = this.getContentResolver();
                try {
                    if (bmp_selectedPhoto != null)// 如果不释放的话,不断取图片,将会内存不够
                        bmp_selectedPhoto.recycle();
                    bmp_selectedPhoto = BitmapFactory.decodeStream(cr
                            .openInputStream(mUri));
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                home_view.setBackground(new BitmapDrawable(getResources(),
                        bmp_selectedPhoto));
  • 相关阅读:
    视图的INSERT、UPDATE、DELETE注意事项
    SQL SERVER 用户管理 TSQL 命令
    SQL SERVER 利用存储过程查看角色和用户信息
    犯错了~
    配置tomcat
    python中的类继承之super
    python中参数解析
    python的几个内联函数:lambda ,zip,filter, map, reduce
    第一次性能测试http_load
    不能在 DropDownList 中选择多个项
  • 原文地址:https://www.cnblogs.com/sharecenter/p/5621092.html
Copyright © 2020-2023  润新知