0

Good Day I'm working in app that captuer image and display it in Gridview but when I click the button I get the following error

java.lang.NullPointerException: file at android.net.Uri.fromFile(Uri.java:452) at CameraFragment.getOutputMediaFileUri(CameraFragment.java:117) at CameraFragment$1.onClick(CameraFragment.java:91)

can anyone help? Here is my code:

   myLists = new ArrayList<Images>();
            adapter = new ImageListAdapter(getActivity(), R.layout.img_list_view, myLists);
            Button myButton = (Button) view.findViewById(R.id.camerabutton);
            myButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);// create a file to save the image
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
                    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE); // start the image capture Intent
                }
            });
            myGridView = (GridView) view.findViewById(R.id.gridView);
            myGridView.setAdapter(adapter);


public Uri getOutputMediaFileUri(int type) {

        return Uri.fromFile(getOutputMediaFile(type));
    }


 public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


        switch (requestCode) {
            case 1:
                if (resultCode == Activity.RESULT_OK) {
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                    cursor.moveToFirst();

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    //file path of captured image
                    String filePath = cursor.getString(columnIndex);
                    //file path of captured image
                    File f = new File(filePath);
                    String filename = f.getName();
                    cursor.close();

                    //Convert file path into bitmap image using below line.
                    Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
                    //put bitmapimage in your imageview
                    //newImageView.setImageBitmap(yourSelectedImage);
                    images = new Images();
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                   // compressing the image
                    Bitmap image =Bitmap.createScaledBitmap(yourSelectedImage,
                            yourSelectedImage.getWidth()/2, yourSelectedImage.getHeight()/2, true);
                    image.compress(Bitmap.CompressFormat.PNG, 50,
                            stream);
                    // convert the image to a byte stream
                    byte[] byte_arr = stream.toByteArray();
                    images.setImageBlob(byte_arr);
                    images.setImageName(filename);

                }  }
        if (resultCode == getActivity().RESULT_CANCELED) {
            // user cancelled Image capture
            Toast.makeText(getActivity(), "User cancelled image capture", Toast.LENGTH_SHORT)
                    .show();
        } else {
            // failed to capture image
            Toast.makeText(getActivity(), "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                    .show();
        }
    } }

private static File getOutputMediaFile(int type) {

        // External sdcard location
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), IMAGE_DIRECTORY_NAME);

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                        + IMAGE_DIRECTORY_NAME + " directory");
                return null;
            }
        }


        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                Locale.getDefault()).format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator
                    + "IMG_" + timeStamp + ".jpg");
        } else {
            return null;
        }
        mCurrentPhotoPath = mediaFile.getAbsolutePath();
        return mediaFile;
    }

0 Answers0