空指针文件URI上的异常? [英] Null pointer Exception on file- URI?

查看:198
本文介绍了空指针文件URI上的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,有一个捕获按钮可以使用设备相机捕获图像,因此我对该按钮的click事件使用了captureImage()方法.当我单击该按钮时,将引发空指针异常.不明白这是怎么发生的,有人可以帮忙吗?预先感谢!

In my app a capture button is there to capture an image using device camera,so I have used a method captureImage() on the click event of that button.When I click the button a null pointer exception is thrown.I could not understand how this happens Can anyone help? Thanks in advance!

Create()方法上的捕获"按钮

capture button on on Create () method

photoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            /*
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);*/
            captureImage();
        }
    });

captureImage()方法

captureImage() method

private void captureImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    // start the image capture Intent
    startActivityForResult(intent, CAMERA_REQUEST);
}

此方法的空指针异常

  public Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

/**
 * returning image / video
 */
private static File getOutputMediaFile(int type) {

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

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(TAG, "Oops! Failed create "
                    + Config.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;
    }

    return mediaFile;
}

Logcat

 java.lang.NullPointerException: file
        at android.net.Uri.fromFile(Uri.java:447)
        at com.riafy.user.imagegallerydemo.MainActivity.getOutputMediaFileUri(MainActivity.java:235)
        at com.riafy.user.imagegallerydemo.MainActivity.captureImage(MainActivity.java:97)
        at com.riafy.user.imagegallerydemo.MainActivity.access$100(MainActivity.java:29)
        at com.riafy.user.imagegallerydemo.MainActivity$2.onClick(MainActivity.java:68)
        at android.view.View.performClick(View.java:4761)
        at android.view.View$PerformClick.run(View.java:19767)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5312)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

推荐答案

getOutputMediaFile(type)返回null.

getOutputMediaFile(type) returns null.

 public Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

您要返回这里:

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

您添加了以下权限

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

出现在你的清单上?

这篇关于空指针文件URI上的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆