我已经创建了一个带有自定义类的绘图应用程序,我将该类命名为" drawView"。 在有人完成绘制之后,他点击了一个名为" saveBtn"的按钮,然后它应该使用php将图像上传到服务器。
请查看下面的代码并帮我弄清楚为什么它不上传图片。
CreateSingleSignature.java: (我只是告诉你主要代码..没有onCreate等。)
public class CreateSigleSignature extends Activity implements OnClickListener{
private MainDrawingView drawView;
Button saveBtn;
InputStream inputStream;
Bitmap bm;
public void onClick(View v) {
switch (v.getId()) {
case R.id.save_btn:
try {
drawView.setDrawingCacheEnabled(true);
bm = Bitmap.createBitmap(drawView.getDrawingCache());
executeMultipartPost();
} catch (Exception e) {
Log.e(e.getClass().getName(), e.getMessage());
}
drawView.destroyDrawingCache();
break;
}
}
public void executeMultipartPost() throws Exception {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(
"http://www.zinman.byethost6.com/up/upload.php");
ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("uploaded", 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();
sResponse = reader.readLine();
AlertDialog alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Upload Information");
alertDialog.setMessage(sResponse);
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
} catch (Exception e) {
// handle exception here
Log.e(e.getClass().getName(), e.getMessage());
}
}
upload.php的:
if(isset($_FILES['uploaded'])){
move_uploaded_file($_FILES['uploaded']['tmp_name'], "bvbvb.jpg");
echo $_FILES['uploaded']['tmp_name'] . " ";
echo $_FILES['uploaded']['name'] . " ";
echo $_FILES['uploaded']['type'] . " ";
echo $_FILES['uploaded']['size'];
$data = file_get_contents($_FILES['uploaded']['tmp_name']);
echo $data;
$image = imagecreatefromstring( $data );
move_uploaded_file($image, "bb.jpg");
echo $image;
}
正如你所看到的,我创建了两个用php上传文件但没有任何工作的选项。 如何解决?