我是初学Android开发者,我需要你的帮助。我尝试使用base64
将图像编码为字符串这是我的base64code。
private String photoToBase64String(String photopath) {
long filelen = (new File(photopath).length()<<32)>>32;
if (1048576 < filelen) {
Log.e("imsmobilememory","Picture too big !");
return "";
}
byte[] rawbytebuffer = new byte[(int)filelen];
StringBuilder sbp = new StringBuilder();
try {
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(photopath));
int res = bis.read(rawbytebuffer, 0, rawbytebuffer.length);
bis.close();
if (res == rawbytebuffer.length) {
sbp.append(Base64.encodeToString(rawbytebuffer, Base64.DEFAULT|Base64.NO_WRAP));
} else {
Log.e("photoToB64Str","Error: Data read != bufferlength should not happen");
return "";
}
} catch (FileNotFoundException e) {
Log.e("info","Photo not found !");
return "";
} catch (IOException ex) {
Log.e("info","Error while reading file !");
return "";
} finally {
}
return sbp.toString();
}
这是我的logcat
01-27 11:15:04.595: E/AndroidRuntime(6368): FATAL EXCEPTION: main
01-27 11:15:04.595: E/AndroidRuntime(6368): java.lang.NullPointerException
01-27 11:15:04.595: E/AndroidRuntime(6368): at java.io.File.fixSlashes(File.java:185)
01-27 11:15:04.595: E/AndroidRuntime(6368): at java.io.File.<init>(File.java:134)
01-27 11:15:04.595: E/AndroidRuntime(6368): at ims.app.mobileorder.EntryTO.photoToBase64String
(EntryTO.java:94)
01-27 11:15:04.595: E/AndroidRuntime(6368): at ims.app.mobileorder.EntryTO.access$7(EntryTO.java:93)
01-27 11:15:04.595: E/AndroidRuntime(6368): at ims.app.mobileorder.EntryTO$4.onClick (EntryTO.java:237)
01-27 11:15:04.595: E/AndroidRuntime(6368): at android.view.View.performClick(View.java:4380)
01-27 11:15:04.595: E/AndroidRuntime(6368): at android.view.View$PerformClick.run(View.java:18094)
01-27 11:15:04.595: E/AndroidRuntime(6368): at android.os.Handler.handleCallback(Handler.java:725)
01-27 11:15:04.595: E/AndroidRuntime(6368): at android.os.Handler.dispatchMessage(Handler.java:92)
01-27 11:15:04.595: E/AndroidRuntime(6368): at android.os.Looper.loop(Looper.java:137)
01-27 11:15:04.595: E/AndroidRuntime(6368): at android.app.ActivityThread.main (ActivityThread.java:5279)
01-27 11:15:04.595: E/AndroidRuntime(6368): at java.lang.reflect.Method.invokeNative(Native Method)
01-27 11:15:04.595: E/AndroidRuntime(6368): at java.lang.reflect.Method.invoke(Method.java:511)
01-27 11:15:04.595: E/AndroidRuntime(6368): at com.android.internal.os. ZygoteInit$MethodAndArgs-
Caller.run (ZygoteInit.java:1102)
01-27 11:15:04.595: E/AndroidRuntime(6368): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:869)
01-27 11:15:04.595: E/AndroidRuntime(6368): at dalvik.system.NativeStart.main(Native Method)