Android 为什么我无法使用相机传递结果信息?
我是安卓开发的不速之客,我很难返回相机拍摄的图像。当我垂直/纵向拍摄照片时,我的应用程序运行正常,但当水平/横向拍摄照片时,应用程序崩溃,出现一个错误,显示未能提供结果ResultInfo{who=null,request=1,result=-1,data=null}。我调整了输出图片的宽度和高度,但没有成功。非常感谢您的帮助 我的代码Android 为什么我无法使用相机传递结果信息?,android,camera,nullpointerexception,cursor,uri,Android,Camera,Nullpointerexception,Cursor,Uri,我是安卓开发的不速之客,我很难返回相机拍摄的图像。当我垂直/纵向拍摄照片时,我的应用程序运行正常,但当水平/横向拍摄照片时,应用程序崩溃,出现一个错误,显示未能提供结果ResultInfo{who=null,request=1,result=-1,data=null}。我调整了输出图片的宽度和高度,但没有成功。非常感谢您的帮助 我的代码 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
//check if we are returning from picture selection
if (requestCode == PICKER) {
//the returned picture URI
// Uri pickedUri = data.getData();
//declare the bitmap
Bitmap pic = null;
//declare the path string
String imgPath = "";
//retrieve the string using media data
String[] medData = { MediaStore.Images.Media.DATA };
//query the data
Cursor picCursor = managedQuery(outputFileUri, medData, null, null, null);
if(picCursor!=null)
{
//get the path string
int index = picCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
picCursor.moveToFirst();
imgPath = picCursor.getString(index);
}
else
imgPath = outputFileUri.getPath();
//if and else handle both choosing from gallery and from file manager
//if we have a new URI attempt to decode the image bitmap
if(outputFileUri!=null) {
//set the width and height we want to use as maximum display
int targetWidth = 600;
int targetHeight = 400;
//sample the incoming image to save on memory resources
//create bitmap options to calculate and use sample size
BitmapFactory.Options bmpOptions = new BitmapFactory.Options();
//first decode image dimensions only - not the image bitmap itself
bmpOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(imgPath, bmpOptions);
//work out what the sample size should be
//image width and height before sampling
int currHeight = bmpOptions.outHeight;
int currWidth = bmpOptions.outWidth;
//variable to store new sample size
int sampleSize = 1;
//calculate the sample size if the existing size is larger than target size
if (currHeight>targetHeight || currWidth>targetWidth)
{
//use either width or height
if (currWidth>currHeight)
sampleSize = Math.round((float)currHeight/(float)targetHeight);
else
sampleSize = Math.round((float)currWidth/(float)targetWidth);
}
//use the new sample size
bmpOptions.inSampleSize = sampleSize;
//now decode the bitmap using sample options
bmpOptions.inJustDecodeBounds = false;
//get the file as a bitmap
pic = BitmapFactory.decodeFile(imgPath, bmpOptions);
if(currentPic<=10){
//pass bitmap to ImageAdapter to add to array
imgAdapt.addPic(pic);
currentPic++;
}
//redraw the gallery thumbnails to reflect the new addition
picGallery.setAdapter(imgAdapt);
//display the newly selected image at larger size
picView.setImageBitmap(pic);
//scale options
picView.setScaleType(ImageView.ScaleType.FIT_CENTER);
}
}
}
//superclass method
super.onActivityResult(requestCode, resultCode, data);
}
当屏幕方向更改时,您的活动将被销毁并在新方向中重新创建。因此,在活动生命周期内获得引用的任何变量将不再引用任何内容,如果您随后尝试访问它们引用的对象而不重新为其赋值,则将获得NullPointerException onSaveInstanceState方法用于在配置更改之间保存临时数据。这将创建一个Bundle,在活动再次启动时传递给onCreate 没有任何代码,我不知道这是否是您的问题,但值得一看 毫无疑问,请参阅以获取比我提供的更多信息和更准确的信息
不是我从其他帖子复制的当你的屏幕方向改变时,你的活动将被销毁并在新的方向中重新创建。因此,在活动生命周期内获得引用的任何变量将不再引用任何内容,如果您随后尝试访问它们引用的对象而不重新为其赋值,则将获得NullPointerException onSaveInstanceState方法用于在配置更改之间保存临时数据。这将创建一个Bundle,在活动再次启动时传递给onCreate 没有任何代码,我不知道这是否是您的问题,但值得一看 毫无疑问,请参阅以获取比我提供的更多信息和更准确的信息
不是我从其他帖子复制的你想在你的应用程序中支持纵向和横向视图吗?如果没有,问题可能通过防止方向改变来解决。在onCreate中放入以下代码:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
仍然可以更改相机意图中的方向。是否要在应用程序中支持纵向和横向视图?如果没有,问题可能通过防止方向改变来解决。在onCreate中放入以下代码:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
仍然可以更改相机意图中的方向。我之前遇到过这个问题,没有看到任何与我的原因相符的答案 问题是: 摄像机接管了一切 设备在照相机模式下旋转 抓拍一张照片并确认 图片将发送回活动,但您的活动/片段将 在屏幕上销毁并重新创建方向更改 包括 存储输出URI文件/字符串的变量 解决方案: 将输出URI存储在onSaveInstanceState方法中,该方法包含用于重新创建活动的捆绑包 在onRestoreInstanceState中为activity检索捆绑包中的文件,在onCreateView中为片段检索捆绑包中的文件
我之前遇到过这个问题,没有找到任何与我的原因相符的答案 问题是: 摄像机接管了一切 设备在照相机模式下旋转 抓拍一张照片并确认 图片将发送回活动,但您的活动/片段将 在屏幕上销毁并重新创建方向更改 包括 存储输出URI文件/字符串的变量 解决方案: 将输出URI存储在onSaveInstanceState方法中,该方法包含用于重新创建活动的捆绑包 在onRestoreInstanceState中为activity检索捆绑包中的文件,在onCreateView中为片段检索捆绑包中的文件