相机活动返回 null android
我正在构建一个应用程序,我想通过默认相机活动捕获图像,然后返回到我的活动并将该图像加载到 ImageView 中。问题是相机活动总是返回 null。在我的 onActivityResult(int requestCode, int resultCode, Intent data) 方法中,我获取的数据为 null。这是我的代码:
public class CameraCapture extends Activity {
protected boolean _taken = true;
File sdImageMainDirectory;
Uri outputFileUri;
protected static final String PHOTO_TAKEN = "photo_taken";
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.cameracapturedimage);
File root = new File(Environment
.getExternalStorageDirectory()
+ File.separator + "myDir" + File.separator);
root.mkdirs();
sdImageMainDirectory = new File(root, "myPicName");
startCameraActivity();
} catch (Exception e) {
finish();
Toast.makeText(this, "Error occured. Please try again later.",
Toast.LENGTH_SHORT).show();
}
}
protected void startCameraActivity() {
outputFileUri = Uri.fromFile(sdImageMainDirectory);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE:
{
if(resultCode==Activity.RESULT_OK)
{
try{
ImageView imageView=(ImageView)findViewById(R.id.cameraImage);
imageView.setImageBitmap((Bitmap) data.getExtras().get("data"));
}
catch (Exception e) {
// TODO: handle exception
}
}
break;
}
default:
break;
}
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState.getBoolean(CameraCapture.PHOTO_TAKEN)) {
_taken = true;
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean(CameraCapture.PHOTO_TAKEN, _taken);
}
}
我做错了什么吗?
I am building an application where I want to capture an image by the default camera activity and return back to my activity and load that image in a ImageView. The problem is camera activity always returning null. In my onActivityResult(int requestCode, int resultCode, Intent data)
method I am getting data as null
. Here is my code:
public class CameraCapture extends Activity {
protected boolean _taken = true;
File sdImageMainDirectory;
Uri outputFileUri;
protected static final String PHOTO_TAKEN = "photo_taken";
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.cameracapturedimage);
File root = new File(Environment
.getExternalStorageDirectory()
+ File.separator + "myDir" + File.separator);
root.mkdirs();
sdImageMainDirectory = new File(root, "myPicName");
startCameraActivity();
} catch (Exception e) {
finish();
Toast.makeText(this, "Error occured. Please try again later.",
Toast.LENGTH_SHORT).show();
}
}
protected void startCameraActivity() {
outputFileUri = Uri.fromFile(sdImageMainDirectory);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE:
{
if(resultCode==Activity.RESULT_OK)
{
try{
ImageView imageView=(ImageView)findViewById(R.id.cameraImage);
imageView.setImageBitmap((Bitmap) data.getExtras().get("data"));
}
catch (Exception e) {
// TODO: handle exception
}
}
break;
}
default:
break;
}
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState.getBoolean(CameraCapture.PHOTO_TAKEN)) {
_taken = true;
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean(CameraCapture.PHOTO_TAKEN, _taken);
}
}
Am I doing anything wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
你之所以犯错,是因为你做事的方式不对。
如果您将额外参数
MediaStore.EXTRA_OUTPUT
与相机意图一起传递,则相机活动会将捕获的图像写入该路径,并且不会在onActivityResult
方法中返回位图。如果您检查您传递的路径,那么您就会知道相机实际上已将捕获的文件写入该路径中。
有关更多信息,您可以关注 这个,这个和这个
You are getting wrong because you are doing it wrong way.
If you pass the extra parameter
MediaStore.EXTRA_OUTPUT
with the camera intent then camera activity will write the captured image to that path and it will not return the bitmap in theonActivityResult
method.If you will check the path which you are passing then you will know that actually camera had write the captured file in that path.
For further information you can follow this, this and this
我正在用另一种方式做。 data.getData() 字段不保证返回 Uri,因此我检查它是否为 null,如果是则图像位于 extras 中。所以代码是 -
我在生产应用程序中使用此代码,并且它正在工作。
I am doing it another way. The data.getData() field is not guaranteed to return a Uri, so I am checking if it's null or not, if it is then the image is in extras. So the code would be -
I am using this code in the production application, and it's working.
我有类似的问题。我注释掉了清单文件中的一些行,这导致缩略图数据返回为空。
您需要满足以下条件才能使其正常工作:
我希望这可以解决您的问题
如果您的手机是三星,则可能与此http://kevinpotgieter.wordpress.com/2011/03/30/null-intent-passed-back-on-samsung-galaxy-tab/
还有另一个 开放问题可能会给出 附加信息
I had a similar problem. I had commented out some lines in my manifest file which caused the thumbnail data to be returned as null.
You require the following to get this to work:
I hope this resolves your issue
If you phone is a Samsung, it could be related to this http://kevinpotgieter.wordpress.com/2011/03/30/null-intent-passed-back-on-samsung-galaxy-tab/
There is another open question which may give additional information
如果您使用 ImageView 来显示 Camera Intent 返回的位图
您需要将 imageview 引用保存在 onSaveInstanceState 中,并稍后在 onRestoreInstanceState 中恢复它。查看下面的 onSaveInstanceState 和 onRestoreInstanceState 代码。
If you are using an ImageView to display the Bitmap returned by Camera Intent
you need to save the imageview reference inside onSaveInstanceState and also restore it later on inside onRestoreInstanceState. Check out the code for onSaveInstanceState and onRestoreInstanceState below.
经过多次搜索:
after many search :
尝试以下代码
Try following code
从相机拍摄时,很少有手机会返回 null。下面的解决方法将解决。确保检查数据是否为空:
While taking from camera few mobiles would return null. The below workaround will solve. Make sure to check data is null:
您可以从发送到相机意图的文件生成位图。请使用此代码...
You can generate bitmap from the file that you send to camera intent. Please use this code...
尝试这个教程。它对我有用,并像往常一样使用宣言和声明中的许可。还
检查权限
https://androidkennel.org/android-camera-access-tutorial/
Try this tutorial. It works for me and use permission as usual in manifesto & also
check permission
https://androidkennel.org/android-camera-access-tutorial/
经过大量的认真研究,我终于得出了结论。
为此,您应该将捕获的图像保存到外部存储中。
然后,边上传边检索。
这样图像分辨率就不会降低,并且您也不会得到 NullPointerException!
我使用时间戳来命名文件,这样我们每次都会得到一个唯一的文件名。
创建文件名的函数:
和 onActivityResult:
编辑:授予相机权限以及在清单中的存储中写入和读取的权限。
After a lot of vigorous research I finally reached to a conclusion.
For doing this, you should save the image captured to the external storage.
And then,retrieve while uploading.
This way the image res doesnt degrade and you dont get NullPointerException too!
I have used timestamp to name the file so we get a unique filename everytime.
Function to create a file name :
and the onActivityResult:
Edit: give permissions for camera and to write and read from storage in your manifest.