照相机拍照NullPointerException 问题

来源:互联网 发布:ip地址端口查询 编辑:程序博客网 时间:2024/05/01 08:18

ACTION_IMAGE_CAPTURE 照相时遇到了NullPointerException问题。

因机器不同,在获得图片的方式上有所不同。

EXTRA_OUTPUT不能将拍下的图片保存到临时文件中,显示时就会出现NullPointerException问题。

根据data直接获得图片的URI地址,可以显出图片。

File tempFile;String TEMP = "temp.jpg";private File getTempFile() {            File dir = new File(Util.getTempImageDirectory());            if (!dir.exists()) {                if (!dir.mkdirs()) {                    return null;                }            }            tempFile = new File(Util.getTempImageDirectory(), TEMP);            try {                tempFile.createNewFile();            } catch (IOException e) {                return null;            }            return tempFile;    }protected void getImagesFromCamera(){        Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);        uri = Uri.fromFile(tempFile);        intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);        intent.putExtra("return-data", true);        startActivityForResult(intent, GET_CODE_ORIGINAL_IMAGE_BY_CAMERA);    }//获得图片protected void onActivityResult(int requestCode, int resultCode, Intent data) {        if (resultCode == RESULT_CANCELED) {        }        else {             BitmapFactory.Options options = new BitmapFactory.Options();             options.inSampleSize = 2;             Bitmap myBitmaptemp = BitmapFactory.decodeFile(tempFile.getAbsolutePath(),options);             if(myBitmaptemp == null){                 Uri imageUri = data.getData();                 ContentResolver cr = getContentResolver();                 InputStream imgIS  = cr.openInputStream(imageUri);                 myBitmaptemp=BitmapFactory.decodeStream(imgIS);             }             myBitmaptemp = zoomBitmap(myBitmaptemp,(int)imgWidth,(int)imgHeight);             .................          }    } //修改图片尺寸 public static Bitmap zoomBitmap(Bitmap bitmap,int w,int h){        int width = bitmap.getWidth();        int height = bitmap.getHeight();        Matrix matrix = new Matrix();        float scaleWidht = ((float)w / width);        float scaleHeight = ((float)h / height);        if(scaleWidht < scaleHeight ){            matrix.postScale(scaleWidht, scaleWidht);        }else{            matrix.postScale(scaleHeight, scaleHeight);        }        Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, width , height , matrix,true);        return newbmp;    }

原创粉丝点击