我正在尝试将图像上传到 PHP 服务器。我已经尝试了超过 48 小时,但没有成功。现在我正在尝试以下操作,但是当我按下发送按钮时,应用程序崩溃了。
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
bm = BitmapFactory.decodeFile("/sdcard/DCIM/android_1.png");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("XXXXXXXXXXXXXXXXXXXXX/uploads/fileup.php");
ByteArrayBody bab = new ByteArrayBody(data, "android_1.jpg");
;
// File file= new File("/mnt/sdcard/forest.png");
// FileBody bin = new FileBody(file);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("uploadedfile", bab);
reqEntity.addPart("photoCaption", new StringBody("sfsdfsdf"));
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();
Toast.makeText(getApplicationContext(), "your file is saved .",Toast.LENGTH_LONG).show();
while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
System.out.println("Response: " + s);
} catch (Exception e) {
// handle exception here
Log.e(e.getClass().getName(), e.getMessage());
}
}
});
这是日志猫:
03-01 12:46:37.638: D/dalvikvm(379): GC_EXTERNAL_ALLOC freed 57K, 49% free 2750K/5379K, external 1625K/2137K, paused 91ms
03-01 12:46:37.797: D/dalvikvm(379): GC_EXTERNAL_ALLOC freed 1K, 49% free 2749K/5379K, external 3125K/3903K, paused 71ms
03-01 12:46:38.557: D/dalvikvm(379): GC_EXTERNAL_ALLOC freed 4K, 49% free 2781K/5379K, external 4813K/4813K, paused 86ms
03-01 12:52:02.326: W/KeyCharacterMap(379): No keyboard for id 0
03-01 12:52:02.326: W/KeyCharacterMap(379): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
03-01 12:52:14.986: D/dalvikvm(379): GC_EXTERNAL_ALLOC freed 192K, 47% free 3050K/5703K, external 5112K/6294K, paused 68ms
03-01 12:52:15.146: D/dalvikvm(379): GC_EXTERNAL_ALLOC freed 7K, 47% free 3043K/5703K, external 6612K/8257K, paused 73ms
03-01 12:52:18.866: D/AndroidRuntime(379): Shutting down VM
03-01 12:52:18.866: W/dalvikvm(379): threadid=1: thread exiting with uncaught exception (group=0x40015560)
03-01 12:52:18.886: E/AndroidRuntime(379): FATAL EXCEPTION: main
03-01 12:52:18.886: E/AndroidRuntime(379): java.lang.NullPointerException: println needs a message
03-01 12:52:18.886: E/AndroidRuntime(379): at android.util.Log.println_native(Native Method)
03-01 12:52:18.886: E/AndroidRuntime(379): at android.util.Log.e(Log.java:230)
03-01 12:52:18.886: E/AndroidRuntime(379): at tracking.android.TrackingActivity$1.onClick(TrackingActivity.java:84)
03-01 12:52:18.886: E/AndroidRuntime(379): at android.view.View.performClick(View.java:2485)
03-01 12:52:18.886: E/AndroidRuntime(379): at android.view.View$PerformClick.run(View.java:9080)
03-01 12:52:18.886: E/AndroidRuntime(379): at android.os.Handler.handleCallback(Handler.java:587)
03-01 12:52:18.886: E/AndroidRuntime(379): at android.os.Handler.dispatchMessage(Handler.java:92)
03-01 12:52:18.886: E/AndroidRuntime(379): at android.os.Looper.loop(Looper.java:130)
03-01 12:52:18.886: E/AndroidRuntime(379): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-01 12:52:18.886: E/AndroidRuntime(379): at java.lang.reflect.Method.invokeNative(Native Method)
03-01 12:52:18.886: E/AndroidRuntime(379): at java.lang.reflect.Method.invoke(Method.java:507)
03-01 12:52:18.886: E/AndroidRuntime(379): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-01 12:52:18.886: E/AndroidRuntime(379): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-01 12:52:18.886: E/AndroidRuntime(379): at dalvik.system.NativeStart.main(Native Method)