ANDROID HUAWEI设备 - >无法获取照片URI

时间:2014-01-26 20:05:19

标签: android nullpointerexception photo

我使用这种方法拍照:

 public void takePhoto () {
        try {
            Log.i(GlobalApplication.APP_LOG_NAMESPACE, "Trying to take photo");
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }

        }   catch (Exception e) {       
            Log.e(GlobalApplication.APP_LOG_NAMESPACE, "takePhoto method cannot be processed", e);
            e.printStackTrace();
        }
    }

在某些设备上,我总是在onActivity中导致图像URI为null(特别是在华为设备上)。

我试图找到任何可行的解决方案,但没有运气。

请有人告诉我,如何解决这个问题?

这是onActivityResultMethod:

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        try {
            Log.i(GlobalApplication.APP_LOG_NAMESPACE, "Trying to process image");
            GlobalApplication globalAppClass = ((GlobalApplication) getApplicationContext());
            AppHelper helper = new AppHelper();
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            Uri imageUri = data.getData();
            if(imageUri == null) {
                Log.i(GlobalApplication.APP_LOG_NAMESPACE, "Image URI is null");
            }
            Bitmap bitmap;
            Bitmap resizedBitmap;
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            String imageFileName = "DAMAGE_" + globalAppClass.getEanCode() + "_" + timeStamp + ".jpg";
            String dirname =  "smartt/"+globalAppClass.getEanCode();

            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
            resizedBitmap = helper.getResizedBitmap(bitmap, 768, 1024);
            helper.createDirectoryAndSaveImage(resizedBitmap, imageFileName, dirname);
            // place thumbnail into image view
            //mImageView.setImageBitmap(imageBitmap);

            TextView photoCountTv = (TextView)findViewById(R.id.damageReportTakenPhotosCountTv);
            photoCountTv.setText(R.string.foto_attached);
            photoCountTv.setTextColor(getResources().getColor(R.color.green));

            TextView dataPassingTv = (TextView)findViewById(R.id.damageReportPassingTitle);
            dataPassingTv.setText(R.string.data_passing);
            dataPassingTv.setTextColor(getResources().getColor(R.color.green));

        } catch (FileNotFoundException e) {
            Log.e(GlobalApplication.APP_LOG_NAMESPACE, "onActivityResult method cannot be processed, file not found", e);
            e.printStackTrace();
        } catch (IOException e) {
            Log.e(GlobalApplication.APP_LOG_NAMESPACE, "onActivityResult method cannot be processed, IOE Exception", e);
            e.printStackTrace();
        } catch (Exception e) {
            Log.e(GlobalApplication.APP_LOG_NAMESPACE, "onActivityResult method cannot be processed, Exception", e);
            e.printStackTrace();
        }
    }
}

感谢您的任何建议

1 个答案:

答案 0 :(得分:0)

我知道这个问题比较老,但我认为很多人都有这个问题。正如@CommonsWare所说,你必须提供你自己的URI:

        photoFile = getOutputMediaFile();
        // Continue only if the File was successfully created
        if (photoFile != null) {
            String fileName = photoFile.getAbsolutePath();
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
             startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }

使用这样的文件:

    public File getOutputMediaFile() {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.
    File mediaStorageDir = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            "Shaufel");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            // base.saveValidationError("MyCameraApp: failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp
            + ".jpg");
    return mediaFile;
}

在您的应用程序中安全地使用fileName(也许还有onSaveInstanceState())

在onActivityResult中

我加载像这样的位图

        public Bitmap decodeSampledBitmapFromResource(Resources res, File file, int reqWidth,
            int reqHeight) throws IOException {
        FileInputStream ino = new FileInputStream(new File(fileName));

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFileDescriptor(ino.getFD(), null, options);
        ino.close();

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(fileName, options);
    }

    public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {

        this.reqHeight = reqHeight;
        this.reqWidth = reqWidth;
        // Raw height and width of image
        this.height = options.outHeight;
        this.width = options.outWidth;
        int inSampleSize = 1;
        if (height > reqHeight || width > reqWidth) {

            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power
            // of 2 and keeps both
            // height and width larger than the requested height and
            // width.
            while ((halfHeight / inSampleSize) > reqHeight
                    || (halfWidth / inSampleSize) > reqWidth) {
                inSampleSize *= 2;
            }
        }

        return inSampleSize;
    }