获得空指针异常,同时捕捉和保存图像使用Android模拟器 [英] Getting Null pointer exception while capturing and saving an image using android emulator

查看:135
本文介绍了获得空指针异常,同时捕捉和保存图像使用Android模拟器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图捕捉,并通过Android模拟器保存图像,图像被捕获,但在保存文件已损坏。

什么引起的?谁能帮我找出可能的错误点?

下面是我的code:

  **公共无效的onCreate **(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    ** **意向意图=新** **意向(android.media.action.IMAGE_CAPTURE);
    尝试{
        了fileURI = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
    }    赶上(IOException异常五){
        // TODO自动生成catch块
        e.printStackTrace();
    }
    intent.putExtra(** MediaStore ** EXTRA_OUTPUT,fileURI所);
    startActivityForResult(意向,CAPTURE_IMAGE_ACTIVITY_REQUEST_ code);
}@覆盖
**保护无效**的onActivityResult(INT申请code,INT结果code,意图数据){
    如果(要求code == CAPTURE_IMAGE_ACTIVITY_REQUEST_ code){
        如果(结果code == RESULT_OK){
            StoreImage(这一点,data.getData()
            媒体文件);
            完();
        }        否则,如果(结果code == RESULT_CANCELED){
            //用户取消了图像采集
        }        其他{
            完();
            尝试{
            }            赶上(例外五){
               e.printStackTrace();
            }
        }
    }}

堆栈跟踪低于::

  04-05 23:55:40.369:E / AndroidRuntime(534):致命异常:主要
04-05 23:55:40.369:E / AndroidRuntime(534):了java.lang.RuntimeException:无法启动活动ComponentInfo {} camera.android/camera.android.CameraActivity:显示java.lang.NullPointerException:文件
04-05 23:55:40.369:E / AndroidRuntime(534):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)04-05 23:55:40.369:E / AndroidRuntime(534):显示java.lang.NullPointerException:产生的原因文件
04-05 23:55:40.369:E / AndroidRuntime(534):在android.net.Uri.fromFile(Uri.java:441)
04-05 23:55:40.369:E / AndroidRuntime(534):在camera.android.CameraActivity.getOutputMediaFileUri(CameraActivity.java:72)
04-05 23:55:40.369:E / AndroidRuntime(534):在camera.android.CameraActivity.onCreate(CameraActivity.java:34)
04-05 23:55:40.369:E / AndroidRuntime(534):在android.app.Activity.performCreate(Activity.java:4465)
04-05 23:55:40.369:E / AndroidRuntime(534):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-05 23:55:40.369:E / AndroidRuntime(534):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)


解决方案

检查您是否在清单文件中添加以下权限:

 <使用特征的android:NAME =android.hardware.camera/>
<使用特征的android:NAME =android.hardware.camera.autofocus/>
<使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/>
<使用许可权的android:NAME =android.permission.CAMERA/>

也在

  = fileURI所getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

我想你一定附上您要保存太文件名,只给路径是不够的......

I am trying to capture and save an image through an Android emulator, image is being captured, but the file being saved is corrupted.

What could be causing this? can anyone help me identify possible error points?

Below is my code:

**public void onCreate**(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    **Intent** intent = new **Intent**("android.media.action.IMAGE_CAPTURE");
    try {
        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
    } 

    catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    intent.putExtra(**MediaStore**.EXTRA_OUTPUT, fileUri);
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

@Override
**protected void onActivityResult**(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            StoreImage(this, data.getData(),
            mediaFile);
            finish();
        } 

        else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } 

        else {
            finish();
            try {
            } 

            catch (Exception e) {
               e.printStackTrace();
            }
        }
    }

}

The Stack trace is below::

04-05 23:55:40.369: E/AndroidRuntime(534): FATAL EXCEPTION: main
04-05 23:55:40.369: E/AndroidRuntime(534): java.lang.RuntimeException: Unable to start activity ComponentInfo{camera.android/camera.android.CameraActivity}: java.lang.NullPointerException: file
04-05 23:55:40.369: E/AndroidRuntime(534):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)

04-05 23:55:40.369: E/AndroidRuntime(534): Caused by: java.lang.NullPointerException: file
04-05 23:55:40.369: E/AndroidRuntime(534):  at android.net.Uri.fromFile(Uri.java:441)
04-05 23:55:40.369: E/AndroidRuntime(534):  at camera.android.CameraActivity.getOutputMediaFileUri(CameraActivity.java:72)
04-05 23:55:40.369: E/AndroidRuntime(534):  at camera.android.CameraActivity.onCreate(CameraActivity.java:34)
04-05 23:55:40.369: E/AndroidRuntime(534):  at android.app.Activity.performCreate(Activity.java:4465)
04-05 23:55:40.369: E/AndroidRuntime(534):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-05 23:55:40.369: E/AndroidRuntime(534):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)

解决方案

check whether you added the below permission in manifest file::

<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />

also in the

fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

I think you must attach the file name that you want to save too, and giving only the path is not enough....

这篇关于获得空指针异常,同时捕捉和保存图像使用Android模拟器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆