- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个 Activity 可以打开相机 Intent 并在 onActivityResult 方法中抓取照片。三台设备运行良好,没有错误,但仅在 一台设备 中,我在 file 变量上出现空错误。
“尝试在空对象引用上调用虚方法 java.lang.String java.io.File.getPath()”
这就是我实现它的方式。
A) 第一步:打开相机
单击按钮后,我运行以下代码。此代码将:
打开相机 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()方法中抓取图片
检查常量 = Constant.CAMERA_PIC_REQUEST。
从照片中获取EXIF数据,找出旋转照片所需的角度。
设置宽度、高度和角度。创建 .jpg 照片
将创建的图片名称和路径保存到imageArray中
用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/
有人能给我一些代码示例,了解如何从相机捕获“完整”图像,然后在“startActivityForResult”中将其转换为字节,以及显示在 imageView 中的位图。任何帮助将不胜感激。 山姆 最
我有一个带有两个圆的组,当我用平移过渡移动其中一个时,我应该看到静止的一个保持在中心(位于场景图的中间),而另一个移动。取而代之的是,“摄像机”跟随移动的圆圈,使其看起来好像都在分开。 有没有一种方法
我希望停止 Phonegap 将图片保存到 iOS 上的相机胶卷。 我发现了一种可能的解决方案,但我不太喜欢它。该解决方案需要通过删除这行代码来 fork phonegap API。 UIImageW
我最近开始使用 JOGL,我知道如何在 Canvas 上创建和绘制对象,但我找不到有关如何设置和旋转相机的教程或说明。我只找到了源代码,但由于我对此很陌生,所以它没有太大帮助。 有人知道一个好的教程或
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 8 年前。 Improve this ques
我想实现这样的目标: 因此,您捕获和处理的唯一内容是矩形中的区域。我需要这个,因为我想做一些 OCR 而我不想要整个屏幕。 最佳答案 也许可以帮助您这个来源,(在相机预览顶部绘制一个边界框以捕获部分图
我正在开发一个 Android 应用程序,我希望我的应用程序能够拍照并显示它们。我的问题是它制作了图片,将它存储在 SD 卡上但没有在我的应用程序中显示它,ImageView 保持空白。这是我的代码:
我正在研究 Android 相机教程,SDK 11。出于某种原因,我在 handleCameraPhoto() 中得到了一个空指针。我唯一看到的是“无法将结果 ResultInfo{who=null,
我一直在尝试实现此代码,我在其中从相机捕获图像并将其显示在我的图像查看器中,然后可以将其设置为墙纸。它在我的模拟器中运行良好,但在我的手机上,当我拍摄图像并单击“确定”时,它会强制关闭。有什么帮助吗?
我想将预览中的某些像素更改为比其余像素暗一点。假设我希望预览的上半部分比预览的下半部分暗一点。 我试图像这样使用 setPreviewCallback: camera.setPreviewCallba
我想为我的应用程序启用相机/照片库设置。我可以显示警报以编程方式启用这些吗就像应用程序启动时一样。我已附上图片以显示我希望看到我的应用程序的类似设置 对于我的应用程序,我没有看到此设置。 我的问题是这
所以我正在开发类似于相机应用程序的东西。它在原生 OpenGL 纹理上绘制实时相机预览。如果按下按钮,则会以原始形式捕获静态图像(可能比实时预览分辨率更高)。 这是我的代码的大纲。为了清楚起见,我没有
我想在我的 Android 应用程序中启动相机 Activity ,我知道该怎么做。我想问一下当相机 Activity 结束时,我如何检查它是否是用户拍摄的照片或视频? 已更新 我有一个对话框,其中询
我在横向模式下有自定义全屏摄像头。设备尺寸为 1024 x 600。 支持的预览尺寸列表 宽 x 高 176 x 144 320 x 240 352 x 288 528 x 432 640 x 480
请看下图!! 所以在这张图片中...您可以看到底部的弹出窗口,当用户“点击”“相机”按钮时,它会为用户提供 3 个选项。 那是什么观点?它是一个模态视图 Controller ,在选择一个选项时被关闭
我发布了一个应用程序,其中一项基本功能是允许用户拍照,然后将该照片保存在其外部存储上的特定文件夹中。 一切似乎都运行良好,但我现在收到两份报告,声称在拍照后单击“完成”退出相机(并返回到 Activi
我在尝试实时更换相机时遇到问题,它适用于本地视频,但远程人员看不到新相机,但仍然可以看到旧相机。我试图停止流并再次初始化,但仍然无法正常工作。这只是我的一些代码。 我到处搜索,但找不到解决方案。有人可
一个简单但非常复杂的问题:为 Flutter 相机添加点击对焦功能的最佳方法是什么? 我在整个万维网上搜索了优雅的解决方案,但一无所获。 你有想法吗? 最佳答案 我可能会迟到,但你可以试试 adv_c
我正在尝试使用 gluLookAt 方法设置 3D 相机。所以我有一个 10x10x10 的立方体,现在我想在那个立方体内移动相机。我有这样的事情: gluLookAt( camera->x,came
我赠送一个 UIImagePickerController在我的应用程序中通过在 sheet 中呈现逻辑修饰符。简而言之,以下三种类型处理显示和关闭 UIImagePickerController 的
我是一名优秀的程序员,十分优秀!