java - Android程序崩溃空指针异常

标签 java android nullpointerexception crash

嘿,我已经尝试解决这个问题几个小时了,并广泛检查了许多其他问题,试图解决这个问题。

因此,在我的主要 Activity 中,我有两个 Activity ,它们要么从图库中获取图像,要么根据按钮获取照片

public void DoTakePhoto(View v) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(intent, TAKE_PICTURE);
        }
    }

public void DoShowSelectImage(View v) {
    Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, SELECT_PICTURE);
}

所以我知道问题出在我的 onActivityResult 上,其中数据似乎为空,我尝试使用 super 来恢复丢失的 Activity 或检查数据是否为空

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);


        if (requestCode == SELECT_PICTURE || requestCode == TAKE_PICTURE  && null != data) {
            if (resultCode == RESULT_OK && null != data) {
                Uri selectedimage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA};

                Cursor cursor = getContentResolver().query(selectedimage, filePathColumn, null, null, null);
                cursor.moveToFirst();

                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                mImageFullPathAndName = cursor.getString(columnIndex);
                cursor.close();
                jobID = "";
                File file = new File(mImageFullPathAndName);
                Bitmap mCurrentSelectedBitmap = decodeFile(file);

                if (mCurrentSelectedBitmap != null) {

                    ivSelectedImg.setImageBitmap(mCurrentSelectedBitmap);


                    int w = mCurrentSelectedBitmap.getWidth();
                    int h = mCurrentSelectedBitmap.getHeight();

                    int length = (w > h) ? w : h;
                    if (length > OPTIMIZED_LENGTH) {
                        float ratio = (float) w / h;
                        int newW, newH = 0;

                        if (ratio > 1.0) {
                            newW = OPTIMIZED_LENGTH;
                            newH = (int) (OPTIMIZED_LENGTH / ratio);
                        } else {
                            newH = OPTIMIZED_LENGTH;
                            newW = (int) (OPTIMIZED_LENGTH * ratio);
                        }
                        mCurrentSelectedBitmap =     rescaleBitmap(mCurrentSelectedBitmap, newW, newH);
                    }

                    mImageFullPathAndName = SaveImage(mCurrentSelectedBitmap);
                }
            }
        } 
    }

所以我的错误

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.zola.capchem/com.example.zola.capchem.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.getScheme()' on a null object reference

我已经尝试了该网站上的大部分内容,但该应用程序仍然最终崩溃。不知道在这里做什么。

最佳答案

如果您尝试从相机拍照,您的代码将会崩溃。

通过相机拍照和从图库/文件系统获取照片的方法有很大不同。

要同时执行这两项操作,您需要使用 ACTION 和一些附加功能来激发 Intent 。您出于此目的启动了一项 Activity 以获得结果。结果通过传递给 onActivityResult() 的 Intent 返回给您。

但是,两种情况下返回 Intent 中存储的结果是不同的。

1) 通过相机拍照:所拍摄图像的位图本身将作为Intent bundle 中的额外内容返回给您。您可以通过以下方式访问它:

Bundle extras = data.getExtras();
Bitmap bitmap = (Bitmap) extras.get("data");
//use bitmap however you like...

2) 从图库中选择照片:您可能会也可能不会获得所选图像的 URI(通过 data.getUri())。如果您获得 URI,则可以通过该 URI 获取图像。然而,这个 uri 有时可能为空,在这种情况下,android 系统选择将图像写入到您在启动结果 Activity 时作为 Intent extra 传递的图像 URI。 因此,首先定义一个临时 URI 并使用它来启动从图库中选择图像的 Activity :

private URI getTempFile()
    {
        if (isExternalStorageWritable())
        {
            File file = new File(Environment.getExternalStorageDirectory(), "temporary_file.jpg");
            try
            {
                if(!file.exists())
                {
                    file.createNewFile();
                }
            } catch (IOException e)
            {
            }
            return Uri.fromFile(file);
        } else
        {
            return null;
        }
    }

public void DoShowSelectImage(View v) {
    Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    i.putExtra(MediaStore.EXTRA_OUTPUT, getTempFile());
    startActivityForResult(i, SELECT_PICTURE);
}

在你的 onActivityResult 中:

Uri selectedimage = data.getData();
if(selectedimage == null)
{
    selectedimage = getTempFile();
}

您需要在 onActivityResult 中单独处理这两种情况:

if(result == RESULT_OK && data != null)
{
    Bitmap mCurrentSelectedBitmap;
    if(requestCode == SELECT_PICTURE)
    {
        Uri selectedimage = data.getData();
        if(selectedimage == null)
        {
            selectedimage = getTempFile();
        }
        ......
        ......
        mCurrentSelectedBitmap = decodeFile(file);
    }
    else if(requestCode == TAKE_PICTURE)
    {
        Bundle extras = data.getExtras();
        mCurrentSelectedBitmap = (Bitmap) extras.get("data");
    }
    .......
    .......
    //Do your thing
}

注意:您可能需要在 list 中添加权限才能写入外部存储

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

关于java - Android程序崩溃空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36675398/

相关文章:

java - 一起使用 JavaCV 和 Realm 会导致 "java.lang.UnsatisfiedLinkError"

java - 当同一库的多个版本被声明为依赖项时,Maven 项目中会加载哪个类?

java - Collections.shuffle 不适用于Processing.js?

android - USB摄像头连接到安卓手机

android - 以用户定义的频率同步选定的帐户

java - 创建添加到 ArrayList 的方法时出现 NullPointerException

java - 内存队列服务器

android - 这可以在android中发送短信时隐藏/更改发件人手机号码吗?

java - 什么是NullPointerException,我该如何解决?

java - Android 空指针异常 ListFragment