-1

Hey so I have been trying to fix this problem for a couple of hours and checked a lot of the other questions extensively in an attempt to remedy this problem.

So in my Main Activity I have two activities that both either grabs an image from the gallery or is a photo depending on the button

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);
}

So I know the problem likes on my onActivityResult where the data seems to be null I tried using the super so it gets back the lost activity or checking to see if data is

@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);
                }
            }
        } 
    }

So my Error

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

I have tried most of the things on this site but the app still ends up crashing. No clue what to do here.

Suliman Sharif
  • 607
  • 1
  • 9
  • 26

1 Answers1

1

Your code would crash if you try to take a picture from the camera.

The method for taking a photo via camera and obtaining a photo from the gallery/file system is quite different.

For doing both, you fire an intent with an ACTION and some extras. You launch an activity for a result with this intent. The result is returned to you in onActivityResult() via the intent passed to it.

However, the result stored in the returned intent is different for both cases.

1) Taking a photo through camera: The bitmap itself of the image taken is returned to you, as an extra in the intent bundle. You can access it as:

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

2) Select a photo from gallery: You may or may not get a URI of the selected image (via data.getUri()). In case you get an URI, you can obtain your image through that URI. However, this uri may be null sometimes, in which case the android system has chosen to write the image on to the image URI that you had passed as an intent extra while launching the activity for result. Hence, first, define a temporary URI and use it to launch the activity to select image from gallery:

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);
}

and inside your onActivityResult:

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

You need to handle both these cases seperately in your 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
}

NOTE: you might need to add permission in your manifest to write to external storage

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Manoj Madanmohan
  • 1,214
  • 1
  • 12
  • 14
  • Thank you, it took me a day to figure it out with your code but it did resolve the error. – Suliman Sharif Apr 18 '16 at 23:07
  • Your welcome. Thanks for accepting this answer. Could you please upvote this answer as well? I'm a bit new to stackoverflow and I need some reputation to be able to comment everywhere. – Manoj Madanmohan Apr 19 '16 at 07:52