无法将位图设置为图像按钮?

时间:2014-03-04 05:30:06

标签: android bitmap sharedpreferences

03-04 11:49:54.672: W/dalvikvm(1640): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-04 11:49:54.702: E/AndroidRuntime(1640): FATAL EXCEPTION: main
03-04 11:49:54.702: E/AndroidRuntime(1640): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.speedlink/com.speedlink.topmenu.SPDTeamTop}: java.lang.NullPointerException
03-04 11:49:54.702: E/AndroidRuntime(1640):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
03-04 11:49:54.702: E/AndroidRuntime(1640):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
03-04 11:49:54.702: E/AndroidRuntime(1640):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-04 11:49:54.702: E/AndroidRuntime(1640):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
03-04 11:49:54.702: E/AndroidRuntime(1640):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-04 11:49:54.702: E/AndroidRuntime(1640):     at android.os.Looper.loop(Looper.java:137)
03-04 11:49:54.702: E/AndroidRuntime(1640):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-04 11:49:54.702: E/AndroidRuntime(1640):     at java.lang.reflect.Method.invokeNative(Native Method)
03-04 11:49:54.702: E/AndroidRuntime(1640):     at java.lang.reflect.Method.invoke(Method.java:511)
03-04 11:49:54.702: E/AndroidRuntime(1640):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-04 11:49:54.702: E/AndroidRuntime(1640):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-04 11:49:54.702: E/AndroidRuntime(1640):     at dalvik.system.NativeStart.main(Native Method)
03-04 11:49:54.702: E/AndroidRuntime(1640): Caused by: java.lang.NullPointerException
03-04 11:49:54.702: E/AndroidRuntime(1640):     at com.speedlink.topmenu.SPDTeamTop.onCreate(SPDTeamTop.java:79)
03-04 11:49:54.702: E/AndroidRuntime(1640):     at android.app.Activity.performCreate(Activity.java:5104)
03-04 11:49:54.702: E/AndroidRuntime(1640):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-04 11:49:54.702: E/AndroidRuntime(1640):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
03-04 11:49:54.702: E/AndroidRuntime(1640):     ... 11 more

从共享首选项中检索图像base64并设置为图像按钮时出现此错误。我将Image Path更改为位图,将bitmap更改为base64tostring以共享首选项存储,Image path和base64tostring已激活,但将image bitmap设置为imagebutton有错误...这是我的代码。

 bitmap = BitmapFactory.decodeStream((InputStream) new URL(url).getContent());
 Bitmap resized = Bitmap.createScaledBitmap(bitmap, 100, 100, false);
 edit.putString("TeamPhotoSaved", encodeTobase64(resized));

这是存储在共享首选项中。

     public static String encodeTobase64(Bitmap image) {
        Bitmap immage = image;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        immage.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] b = baos.toByteArray();
        String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);

        Log.d("Image Log:", imageEncoded);
        return imageEncoded;
    }

 public static Bitmap decodeBase64(String input) {
     byte[] decodedByte = Base64.decode(input, 0);
     return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
 }    

这是位图解码和编码......

 bitmap=Login.decodeBase64(teamphotoSaved);     
 imgbtn_community.setImageBitmap(bitmap);

这是设置为图像按钮。

编辑:Top.java

 SharedPreferences userDetails = context.getSharedPreferences("userdetails", MODE_PRIVATE);
teamphotoSaved = userDetails.getString("TeamPhotoSaved", "");
bitmap=Login.decodeBase64(teamphotoSaved);
imgbtn_community.setImageBitmap(bitmap);

1 个答案:

答案 0 :(得分:1)

请勿使用 getSharedPreference 传递上下文。它不是强制性的。所以必须改变。因为你的上下文变量可能是null。

SharedPreferences userDetails = context.getSharedPreferences("userdetails", MODE_PRIVATE); 

SharedPreferences userDetails = getSharedPreferences("userdetails", MODE_PRIVATE);

必须确保当您尝试以共享偏好方式存储数据时,当您在其中检索和存储数据时,密钥和共享首选项模式必须相同。因此,当您存储数据时,这将是相同的< / p>

SharedPreferences userDetails = getSharedPreferences("userdetails", MODE_PRIVATE);