android位图让Uri无法正常工作

时间:2013-11-24 12:00:48

标签: android bitmap uri

为什么我会做一个简单的转换行为问题?

我原本以为这是我图片文件来源的问题。 后来的测试是正常的显示图片setimageview。 我不明白为什么这里会发生错误?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.edit_main);
    is =  this.getResources().openRawResource(R.drawable.smp);
    BitmapFactory.Options options=new BitmapFactory.Options();
    options.inJustDecodeBounds = false;
    options.inSampleSize = 4;
    Bitmap btp =BitmapFactory.decodeStream(is,null,options);

转换行为

    uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(),btp,  "123","456"));

}

错误代码:

11-24 20:16:17.682  31134-31134/com.example.cacara E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cacara/com.example.cacara.sds}: java.lang.NullPointerException: uriString
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
            at android.app.ActivityThread.access$700(ActivityThread.java:140)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4921)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException: uriString
            at android.net.Uri$StringUri.<init>(Uri.java:464)
            at android.net.Uri$StringUri.<init>(Uri.java:454)
            at android.net.Uri.parse(Uri.java:426)
            at com.example.cacara.sds.onCreate(sds.java:49)
            at android.app.Activity.performCreate(Activity.java:5188)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
            at android.app.ActivityThread.access$700(ActivityThread.java:140)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4921)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

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

我找到原因 原因是没有增加读写权限...... 但我仍然不明白为什么需要这个许可uri