gpt4 book ai didi

安卓相机 : Empty file in onActivityResult method

转载 作者:太空狗 更新时间:2023-10-29 14:06:04 26 4
gpt4 key购买 nike

我有一个 Activity 可以打开相机 Intent 并在 onActivityResult 方法中抓取照片。三台设备运行良好,没有错误,但仅在 一台设备 中,我在 file 变量上出现空错误。

“尝试在空对象引用上调用虚方法 java.lang.String java.io.File.getPath()

这就是我实现它的方式。

A) 第一步:打开相机

单击按钮后,我运行以下代码。此代码将:

  1. 创建一个新文件
  2. 打开相机 Intent

        //create file
    imgName = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()) + ".jpg";
    //global variable
    file = new File(Environment.getExternalStorageDirectory(), imgName);

    if(file != null){
    //save image here

    Uri relativePath = Uri.fromFile(file);

    //Open camera
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, relativePath);
    startActivityForResult(intent, Constant.CAMERA_PIC_REQUEST);
    }

B) 第二步:拍照并点击保存按钮

C) 第三步:在onActivityResult()方法中抓取图片

  1. 检查常量 = Constant.CAMERA_PIC_REQUEST。

  2. 从照片中获取EXIF数据,找出旋转照片所需的角度。

  3. 设置宽度、高度和角度。创建 .jpg 照片

  4. 将创建的图片名称和路径保存到imageArray中

  5. 用imageArray设置 GridView

    public void onActivityResult(int requestCode, int resultCode, Intent data) {


    if (resultCode == RESULT_OK) {

    switch (requestCode) {

    case Constant.CAMERA_PIC_REQUEST:
    try {

    //Get EXIF data and rotate photo (1. file.getPath() has been used)
    ExifInterface exif = new ExifInterface(file.getPath());
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

    int angle = 0;// Landscape

    //rotate photo
    if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
    angle = 90;
    } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
    angle = 180;
    } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
    angle = 270;
    }

    Matrix matrix = new Matrix();
    matrix.postRotate(angle);


    try {

    //Create the rotated .jpg file (2. file.getPath() has been used)
    Bitmap correctBmp = Func.decodeSampleBitmapFromFile(file.getPath(), Constant.PHOTO_WIDTH, Constant.PHOTO_HEIGHT);
    correctBmp = Bitmap.createBitmap(correctBmp, 0, 0, correctBmp.getWidth(), correctBmp.getHeight(), matrix, true);

    FileOutputStream fOut;

    fOut = new FileOutputStream(file);
    correctBmp.compress(Bitmap.CompressFormat.JPEG, 80, fOut);
    fOut.flush();
    fOut.close();

    //image saved successfully - add photo name into reference array. Later I will use this
    //(3. file.getPath() has been used)
    Image img = new Image();
    img.setImageName(imgName);
    img.setAbsolutePath(file.getAbsolutePath());
    img.setRelativePath(Uri.fromFile(file));
    imageArray.add(img);//save in array


    } catch (FileNotFoundException e) {
    Log.e("Error", e.getMessage());

    } catch (IOException e) {
    Log.e("Error", e.getMessage());

    }

    } catch (Exception e) {
    //This exception is triggering.
    Log.e("Error 15", e.getMessage());
    }

    //set this photo in a grid view for user to see
    setGridView(imageArray);
    break;

    case Constant.POPUPFRAG:
    //something else
    break;
    default:
    //deafult
    break;

    }
    }
    }

错误 15(第三个异常)仅在一部手机中触发。我不确定为什么会这样。它显示了我在上面发布的空指针异常。

可以运行上述代码的三个设备至少具有 android v4.0 或更高版本。给我 null 异常的手机是 Samsung Galaxy Note 3 Android v5.0。 (虽然代码失败了,但我可以在手机图库中找到照片)

你能告诉我这里可能出什么问题吗?我错过了什么吗?无论如何要改进这段代码?

谢谢!

最佳答案

您可以这样使用,而不是直接使用 file.getPath()

创建 Uri 的全局变量,然后在所有 Activity 中使用它。

private Uri imageToUploadUri;
file = new File(Environment.getExternalStorageDirectory(), imgName);

if(file != null){
//save image here

imageToUploadUri = Uri.fromFile(file);

//Open camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageToUploadUri);
startActivityForResult(intent, Constant.CAMERA_PIC_REQUEST);
}

然后在您的 onActivityResult() 中像这样使用:

public void onActivityResult(int requestCode, int resultCode, Intent data) {


if (resultCode == RESULT_OK) {

switch (requestCode) {

case Constant.CAMERA_PIC_REQUEST:
try {


ExifInterface exif = new ExifInterface(imageToUploadUri.getPath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

int angle = 0;// Landscape

//rotate photo
if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
angle = 90;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
angle = 180;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
angle = 270;
}

Matrix matrix = new Matrix();
matrix.postRotate(angle);


try {

//Create the rotated .jpg file (2. file.getPath() has been used)
Bitmap correctBmp = Func.decodeSampleBitmapFromFile(imageToUploadUri.getPath(), Constant.PHOTO_WIDTH, Constant.PHOTO_HEIGHT);
correctBmp = Bitmap.createBitmap(correctBmp, 0, 0, correctBmp.getWidth(), correctBmp.getHeight(), matrix, true);

FileOutputStream fOut;

fOut = new FileOutputStream(file);
correctBmp.compress(Bitmap.CompressFormat.JPEG, 80, fOut);
fOut.flush();
fOut.close();

//image saved successfully - add photo name into reference array. Later I will use this
//(3. file.getPath() has been used)
Image img = new Image();
img.setImageName(imgName);
img.setAbsolutePath(file.getAbsolutePath());
img.setRelativePath(imageToUploadUri);
imageArray.add(img);//save in array


} catch (FileNotFoundException e) {
Log.e("Error", e.getMessage());

} catch (IOException e) {
Log.e("Error", e.getMessage());

}

} catch (Exception e) {
//This exception is triggering.
Log.e("Error 15", e.getMessage());
}

//set this photo in a grid view for user to see
setGridView(imageArray);
break;

case Constant.POPUPFRAG:
//something else
break;
default:
//deafult
break;

}
}
}

希望对你有帮助!

关于安卓相机 : Empty file in onActivityResult method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32263326/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com