保存图片文件Uri错误,保存空指针异常

时间:2019-02-28 09:04:36

标签: android save uri

我正在尝试保存编辑后的图像,但我不断收到空指针异常错误。这是我的OnClick保存按钮。我正在获取的图像位于drawView和framelayout内部。

 public void save(View v) {

    String name = mImageName;
    if (name.indexOf(".") > 0) {
        name = name.substring(0, name.lastIndexOf(".")) + "_e";
    }
    MediaStoreSaver.save(mImage2,
            mImagePath,
            name,
            this,
            MediaStoreSaver.TYPE_JPG);
    Toast.makeText(this, "Saved " + name, Toast.LENGTH_SHORT).show();

这是我的MediaStoreSave类。知道我做错了什么吗?

public class MediaStoreSaver {
    public static final byte TYPE_PNG = 2;
    public static final byte TYPE_JPG = 3;
public static final String save(Bitmap bitmap,
                                String folderName,
                                String imageName,
                                Context mContext,
                                byte imageType) {
    File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    String folder_path = folder.getAbsolutePath();
    String file_path = folder_path + "/" + folderName;
    File dir = new File(file_path);

    if (!dir.exists()) {
        dir.mkdirs();
    }
    File file = null;
    try {
        String suffix = ".png";
        Bitmap.CompressFormat format = Bitmap.CompressFormat.PNG;
        if (imageType == TYPE_JPG) {
            suffix = ".jpg";
            format = Bitmap.CompressFormat.JPEG;
        }

        file = File.createTempFile(imageName, suffix, dir);
        FileOutputStream fOut = new FileOutputStream(file);
        bitmap.compress(format, 100, fOut);
        System.out.println("saved image: " + file.getAbsolutePath());
        fOut.flush();
        fOut.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    MediaStorageScan(mContext, file);
    return file.getAbsolutePath();
}

/*
 * Refresh image files to view them on computer
 */
private static void MediaStorageScan(Context context, final File file) {
    final Uri fileUri = Uri.fromFile(file);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        context.sendBroadcast(new Intent("android.hardware.action.NEW_PICTURE", fileUri));
    }

    context.sendBroadcast(new Intent("com.android.camera.NEW_PICTURE", fileUri));

    final Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
    intent.setData(fileUri);
    context.sendBroadcast(intent);
}

}

这是我的logcat消息:

  

2019-02-28 16:38:59.512 15830-15830 / rs.example.android.com.healingbrush E / Android运行时:致命例外:主要
      流程:rs.example.android.com.healingbrush,PID:15830
      java.lang.IllegalStateException:无法执行android:onClick
的方法     在android.view.View $ DeclaredOnClickListener.onClick(View.java:4827)
    在android.view.View.performClick(View.java:5773)
    在android.view.View $ PerformClick.run(View.java:23035)
    在android.os.Handler.handleCallback(Handler.java:836)
    在android.os.Handler.dispatchMessage(Handler.java:103)
    在android.os.Looper.loop(Looper.java:232)
    在android.app.ActivityThread.main(ActivityThread.java:6802)
    在java.lang.reflect.Method.invoke(本机方法)
    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1103)
    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)

     

由以下原因引起:java.lang.reflect.InvocationTargetException
    在java.lang.reflect.Method.invoke(本机方法)
    在android.view.View $ DeclaredOnClickListener.onClick(View.java:4822)
    在android.view.View.performClick(View.java:5773)上
    在android.view.View $ PerformClick.run(View.java:23035)上
    在android.os.Handler.handleCallback(Handler.java:836)上
    在android.os.Handler.dispatchMessage(Handler.java:103)v     在android.os.Looper.loop(Looper.java:232)上
    在android.app.ActivityThread.main(ActivityThread.java:6802)
    在java.lang.reflect.Method.invoke(本机方法)
    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1103)
    com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)

     

原因:java.lang.NullPointerException:文件
    在android.net.Uri.fromFile(Uri.java:452)
    在rs.example.android.com.healingbrush.MediaStoreSaver.MediaStorageScan(MediaStoreSaver.java:77)
    在rs.example.android.com.healingbrush.MediaStoreSaver.save(MediaStoreSaver.java:69)
    在rs.example.android.com.healingbrush.MainActivity.save(MainActivity.java:342)
    在java.lang.reflect.Method.invoke(本机方法)
    在android.view.View $ DeclaredOnClickListener.onClick(View.java:4822)     在android.view.View.performClick(View.java:5773)上
    在android.view.View $ PerformClick.run(View.java:23035)上
    在android.os.Handler.handleCallback(Handler.java:836)上
    在android.os.Handler.dispatchMessage(Handler.java:103)上
    在android.os.Looper.loop(Looper.java:232)上
    在android.app.ActivityThread.main(ActivityThread.java:6802)
    在java.lang.reflect.Method.invoke(本机方法)
    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1103)
    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)上
    2019-02-28 16:38:59.578 1332-1443 /? E / InputDispatcher:通道'cd07d33 rs.example.android.com.healingbrush / rs.example.android.com.healingbrush.MainActivity(服务器)'〜通道不可恢复,将被销毁!
    2019-02-28 16:39:01.733 2583-6309 /? E / resmon:[34:StackTraceElement.run]等待触发器

0 个答案:

没有答案