从GetoutputmediaFile方法返回图像文件

时间:2016-02-20 08:50:23

标签: java android file gridview camera

美好的一天我在app中工作,捕获图像并在Gridview中显示但是当我点击按钮时出现以下错误

java.lang.NullPointerException:在CameraFragment $ 1.onClick(CameraFragment.java:91)上的CameraFragment.getOutputMediaFileUri(CameraFragment.java:117)的android.net.Uri.fromFile(Uri.java:452)上的文件p>

任何人都可以帮忙吗?

这里是我的代码

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

1 个答案:

答案 0 :(得分:0)

    return Uri.fromFile(getOutputMediaFile(type));    

此行发生空指针异常,因为getOutputMediaFile(type)返回null。

因为某种方式目录创建失败并返回null

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