2013-04-30 90 views
2

我試圖將中的IMAGEVIEW不在我的Drawable中的圖像轉換爲Base64字符串,並使用下面的代碼,但它導致NullPointerException將圖像從ImageView轉換爲Base64字符串

下面是代碼:

ImageView iv1 = (ImageView)findViewById(R.id.imageView1); 
iv1.buildDrawingCache(); 
Bitmap bmap = iv1.getDrawingCache(); 
ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
bmap.compress(CompressFormat.PNG,100,bos); 
byte[] bb = bos.toByteArray(); 
String image = Base64.encodeBytes(bb); 

這裏是logcat的說:

04-30 11:38:23.233: E/AndroidRuntime(17284): FATAL EXCEPTION: main 
04-30 11:38:23.233: E/AndroidRuntime(17284): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.imageurl/com.example.imageurl.MainActivity}: java.lang.NullPointerException 
04-30 11:38:23.233: E/AndroidRuntime(17284): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
04-30 11:38:23.233: E/AndroidRuntime(17284): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
04-30 11:38:23.233: E/AndroidRuntime(17284): at android.app.ActivityThread.access$600(ActivityThread.java:130) 
04-30 11:38:23.233: E/AndroidRuntime(17284): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
04-30 11:38:23.233: E/AndroidRuntime(17284): at android.os.Handler.dispatchMessage(Handler.java:99) 
04-30 11:38:23.233: E/AndroidRuntime(17284): at android.os.Looper.loop(Looper.java:137) 
04-30 11:38:23.233: E/AndroidRuntime(17284): at android.app.ActivityThread.main(ActivityThread.java:4745) 
04-30 11:38:23.233: E/AndroidRuntime(17284): at java.lang.reflect.Method.invokeNative(Native Method) 
04-30 11:38:23.233: E/AndroidRuntime(17284): at java.lang.reflect.Method.invoke(Method.java:511) 
04-30 11:38:23.233: E/AndroidRuntime(17284): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
04-30 11:38:23.233: E/AndroidRuntime(17284): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
04-30 11:38:23.233: E/AndroidRuntime(17284): at dalvik.system.NativeStart.main(Native Method) 
04-30 11:38:23.233: E/AndroidRuntime(17284): Caused by: java.lang.NullPointerException 
04-30 11:38:23.233: E/AndroidRuntime(17284): at com.example.imageurl.MainActivity.onCreate(MainActivity.java:59) 
04-30 11:38:23.233: E/AndroidRuntime(17284): at android.app.Activity.performCreate(Activity.java:5008) 
04-30 11:38:23.233: E/AndroidRuntime(17284): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
04-30 11:38:23.233: E/AndroidRuntime(17284): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
+0

什麼是此行MainActivity.java:59 – 2013-04-30 03:56:45

回答

5

我已經修復它。 NullPointerException由此行導致。

iv1.buildDrawingCache(); 
Bitmap bmap = iv1.getDrawingCache(); 

因此,而不是使用上面的代碼,我用下面的代碼。

BitmapDrawable drawable = (BitmapDrawable) iv.getDrawable(); 
Bitmap bitmap = drawable.getBitmap(); 
+0

如果你已經解決了問題,那麼請註明您的答案是正確的,關閉問題的代碼。 – GrIsHu 2013-04-30 04:48:26

+0

@test測試解決我的問題,謝謝 – 2015-05-23 10:27:58

0

檢查這一點,也是工作:

iv1.buildDrawingCache(); 
Bitmap bitmap = iv1.getDrawingCache(); 

ByteArrayOutputStream stream=new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); 
byte[] image=stream.toByteArray(); 
System.out.println("byte array:"+image); 

String img_str = Base64.encodeToString(image, 0); 
System.out.println("string:"+img_str); 
相關問題