Android URI解析无法找到图像

时间:2016-02-03 06:04:57

标签: android bitmap uri

此功能:

public Uri convertURI(Context context, Bitmap bitmap){
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
    String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, "Profile", null);
    return Uri.parse(path);
}

当我通过意图将其作为字符串发送到另一个活动时,创建的URI路径是这样的:

02-02 22:02:54.471 23398-23398/recruitr.recruitr E/cand_uri_string: content://media/external/images/media/76268

现在当我尝试使用uri再次访问图像时,我得到了这个(使用通用图像加载器库) - 它无法找到图像:

02-02 22:04:07.597 24662-24662/recruitr.recruitr E/AndroidRuntime: FATAL EXCEPTION: main
                                                               Process: recruitr.recruitr, PID: 24662
                                                               java.lang.NullPointerException: Attempt to read from field 'com.nostra13.universalimageloader.core.DisplayImageOptions com.nostra13.universalimageloader.core.ImageLoaderConfiguration.defaultDisplayImageOptions' on a null object reference
                                                                   at com.nostra13.universalimageloader.core.ImageLoader.loadImageSync(ImageLoader.java:597)
                                                                   at com.nostra13.universalimageloader.core.ImageLoader.loadImageSync(ImageLoader.java:543)
                                                                   at recruitr.recruitr.CandidateProfile.CandidateProfilePreview_Fragment.onCreateView(CandidateProfilePreview_Fragment.java:84)
                                                                   at android.support.v4.app.Fragment.performCreateView(Fragment.java:1965)
                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1078)
                                                                   at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1259)
                                                                   at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
                                                                   at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1624)
                                                                   at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:570)
                                                                   at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
                                                                   at android.support.v4.view.ViewPager.populate(ViewPager.java:1106)
                                                                   at android.support.v4.view.ViewPager.populate(ViewPager.java:952)
                                                                   at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1474)
                                                                   at android.view.View.measure(View.java:18795)
                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                   at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:610)
                                                                   at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:677)
                                                                   at android.view.View.measure(View.java:18795)
                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                   at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                   at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
                                                                   at android.view.View.measure(View.java:18795)
                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                                   at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                                   at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                                   at android.view.View.measure(View.java:18795)
                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                   at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                   at android.view.View.measure(View.java:18795)
                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465)
                                                                   at android.widget.LinearLayout.measureVertical(LinearLayout.java:748)
                                                                   at android.widget.LinearLayout.onMeasure(LinearLayout.java:630)
                                                                   at android.view.View.measure(View.java:18795)
                                                                   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
                                                                   at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
                                                                   at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2658)
                                                                   at android.view.View.measure(View.java:18795)
                                                                   at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100)
                                                                   at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216)
                                                                   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452)
                                                                   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                                                                   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                                                                   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                                   at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                   at android.view.Choreographer.doFrame(Choreographer.java:606)
                                                                   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                                   at android.os.Handler.handleCallback(Handler.java:739)
                                                                   at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                   at android.os.Looper.loop(Looper.java:148)
                                                                   at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                at java.lang.reflect.Method.

有谁知道为什么会这样?

1 个答案:

答案 0 :(得分:1)

使用Imageview

使用Universal imageloader尝试使用以下代码行在URI上加载图片
private final DisplayImageOptions options;
private final ImageLoader imageLoader;    

imageLoader = ImageLoader.getInstance();

        ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(activity));

        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(R.drawable.loader)
                .showImageForEmptyUri(R.drawable.loader)
                .showImageOnFail(R.drawable.loader)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .build();

您必须使用"file://" displayImage的{​​{1}}方法中的Univeral

Imageloader