Java Android程序崩溃NullPoint异常

Java Android程序崩溃NullPoint异常,java,android,nullpointerexception,crash,Java,Android,Nullpointerexception,Crash,嘿,我已经试着解决这个问题好几个小时了,并且广泛地检查了很多其他问题,试图解决这个问题 所以在我的主要活动中,我有两个活动,要么从画廊中抓取一张图片,要么是一张照片,具体取决于按钮 public void DoTakePhoto(View v) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (intent.resolveActivity(getPackageManager())

嘿,我已经试着解决这个问题好几个小时了,并且广泛地检查了很多其他问题,试图解决这个问题

所以在我的主要活动中,我有两个活动,要么从画廊中抓取一张图片,要么是一张照片,具体取决于按钮

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来恢复丢失的活动,或者检查数据是否是空的

@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

我在这个网站上尝试了很多东西,但应用程序最终还是崩溃了。不知道该在这里做什么。

如果您试图从相机拍照,代码将崩溃

通过相机拍摄照片和从gallery/文件系统获取照片的方法完全不同

为了做到这两个,你用一个动作和一些额外的动作来激发一个意图。您为此目的启动了一项活动。结果将通过传递给它的意图在onActivityResult()中返回给您

但是,存储在返回的意图中的结果对于这两种情况都是不同的。

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系统已选择将图像写入图像uri,您在启动“活动获取结果”时已将图像作为额外目的传递。 因此,首先,定义一个临时URI,并使用它启动从库中选择图像的活动:

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();
}
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
}
您需要在onActivityResult中分别处理这两种情况:

Uri selectedimage = data.getData();
if(selectedimage == null)
{
    selectedimage = getTempFile();
}
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
}
注意:您可能需要在清单中添加写入外部存储的权限

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


尝试此操作在启动活动之前,请将imageURI设置为extras@JhamanDas我刚试过,它返回了同样的错误谢谢你,我花了一天时间用你的代码解决了这个问题,但它确实解决了这个错误。欢迎你。谢谢你接受这个答案。请你也投票表决这个答案好吗?我对stackoverflow有点陌生,我需要一些声誉才能在任何地方发表评论。