Getting Null pointer exception while capturing and saving an image using android emulator

♀尐吖头ヾ 提交于 2019-12-06 03:17:07
Shankar Agarwal

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....

Interchange this line,

if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
                if (resultCode == RESULT_OK) {

to

 if (resultCode == RESULT_OK) {
                    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {

First check whether the child activity is done its work successfully or not.

"@ Agarwal : on calling the onActivityResult"

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

        }

    }

"If the result is success, then the data from intern is stored n file which is created and saved as jpeg format"

public static void StoreImage(Context mContext, Uri imageLoc, File imageDir) {
        Bitmap bm = null;
        try {
            bm = Media.getBitmap(mContext.getContentResolver(), imageLoc);
            FileOutputStream out = new FileOutputStream(imageDir);
            bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
            bm.recycle();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

try below code,

Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

    File file=getOutputMediaFile(1);
    picUri = Uri.fromFile(file); // create
    i.putExtra(MediaStore.EXTRA_OUTPUT,picUri); // set the image file

    startActivityForResult(i, CAPTURE_IMAGE);

where getOutputMediaFile(int) will be,

/** Create a File for saving an image */
private  File getOutputMediaFile(int type){
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "MyApplication");

    /**Create the storage directory if it does not exist*/
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            return null;
        }
    }

    /**Create a media file name*/
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == 1){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".png");
    } else {
        return null;
    }

    return mediaFile;
}

and finally,

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == Activity.RESULT_OK) {
        Intent i;
        switch (requestCode) {
        case CAPTURE_IMAGE:
            //THIS IS YOUR Uri
            Uri uri=picUri; 
            break;
        }
    }   
}

cheers....:)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!