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

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

方法一:

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

try 
 {     
     Intent cameraIntent = new     Intent(MediaStore.ACTION_IMAGE_CAPTURE);   
      startActivityForResult(cameraIntent, CAMERA_WITH_DATA); } catch         (ActivityNotFoundException e) 
      {    
           e.printStackTrace(); 

       }

 

Bundle bundle = data.getExtras();
bmp_selectedPhoto = (Bitmap) bundle.get("data");
if (bmp_selectedPhoto != null)
    bmp_selectedPhoto.recycle();

bmp_selectedPhoto = (Bitmap) data.getExtras().get("data");
 
if(bmp_selectedPhoto != null){
    home_view.setBackground(new BitmapDrawable(getResources(),
    bmp_selectedPhoto));
}

方法二:

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

 
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();
        }

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));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

恋恋西风

up up up

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值