获取图片的Uri在Android Marshmallow

时间:2017-07-07 19:21:37

标签: android bitmap uri android-6.0-marshmallow

我试图从内存中获取位图的Uri。我这样做:

public static Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "title", null);
    return Uri.parse(path);
}

这种方法建议大多数 stackoverflow 问题。它在Nougat上运行良好。但它在棉花糖上崩溃了。这是错误:

  

致命的例外:主要                                                                               过程:com.metronome.caloriemeter,PID:14035                                                                               java.lang.NullPointerException:uriString                                                                                   在android.net.Uri $ StringUri。(Uri.java:480)                                                                                   在android.net.Uri $ StringUri。(Uri.java:470)                                                                                   在android.net.Uri.parse(Uri.java:442)                                                                                   在com.metronome.caloriemeter.utils.BitmapUriProvider.getImageUri(BitmapUriProvider.java:21)                                                                                   在com.metronome.caloriemeter.fragments.SocialFragment.shareOnFacebook(SocialFragment.java:144)                                                                                   at com.metronome.caloriemeter.fragments.SocialFragment.access $ 000(SocialFragment.java:35)                                                                                   at com.metronome.caloriemeter.fragments.SocialFragment $ 1.onTouch(SocialFragment.java:85)                                                                                   在android.view.View.dispatchTouchEvent(View.java:9371)                                                                                   在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)                                                                                   在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)                                                                                   在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)                                                                                   在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)                                                                                   在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)                                                                                   在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)                                                                                   在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)                                                                                   在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)                                                                                   在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)                                                                                   在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)                                                                                   在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)                                                                                   在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)                                                                                   在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)                                                                                   在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)                                                                                   在android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)                                                                                   在android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)                                                                                   在com.android.internal.policy.PhoneWindow $ DecorView.superDispatchTouchEvent(PhoneWindow.java:2410)                                                                                   在com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1744)                                                                                   在android.app.Activity.dispatchTouchEvent(Activity.java:2805)                                                                                   在android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:71)                                                                                   在android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:71)                                                                                   在com.android.internal.policy.PhoneWindow $ DecorView.dispatchTouchEvent(PhoneWindow.java:2371)                                                                                   在android.view.View.dispatchPointerEvent(View.java:9597)                                                                                   在android.view.ViewRootImpl $ ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4234)                                                                                   在android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:4100)                                                                                   在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3646)                                                                                   在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3699)                                                                                   在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3665)                                                                                   在android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:3791)                                                                                   在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3673)                                                                                   在android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:3848)                                                                                   在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3646)                                                                                   在android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:3699)                                                                                   在android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:3665)                                                                                   在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:3673)                                                                                   在android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:3646)                                                                                   在android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5926)                                                                                   在android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5900)                                                                                   在android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5861)                                                                                   在android.view.ViewRootImpl $ WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:6029)                                                                                   在android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)                                                                                   在android.os.MessageQueue.nativePollOnce(本机方法)                                                                                   在android.os.MessageQueue.next(MessageQueue.java:331)                                                                                   在android.os.Looper.loop(Looper.java:218)                                                                                   在android.app.ActivityThread.main(ActivityThr

我做错了什么吗?或者至少还有其他方法来获取位图吗?

0 个答案:

没有答案