我正在尝试使用php将摄像头捕获的图像上传到服务器,但点击上传按钮会转到之前的活动我无法解决问题所在。
这里是UploadActivity.java
package com.example.opts;
import java.io.File;
import java.io.IOException;
import com.example.opts.AndroidMultiPartEntity.ProgressListener;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;;
public class UploadActivity extends Activity {
// LogCat tag
private static final String TAG = MainActivity.class.getSimpleName();
private ProgressBar progressBar;
private String filePath = null;
private TextView txtPercentage;
private ImageView imgPreview;
private Button btnUpload;
long totalSize = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.upload_activity);
txtPercentage = (TextView) findViewById(R.id.txtPercentage);
btnUpload = (Button) findViewById(R.id.btnUpload);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
imgPreview = (ImageView) findViewById(R.id.imgPreview);
// Changing action bar background color
// Receiving the data from previous activity
Intent i = getIntent();
// image or video path that is captured in previous activity
filePath = i.getStringExtra("filePath");
// boolean flag to identify the media type, image or video
boolean isImage = i.getBooleanExtra("isImage", true);
if (filePath != null) {
// Displaying the image or video on the screen
previewMedia(isImage);
} else {
Toast.makeText(getApplicationContext(),
"Sorry, file path is missing!", Toast.LENGTH_LONG).show();
}
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// uploading the file to server
new UploadFileToServer().execute();
}
});
}
/**
* Displaying captured image/video on the screen
* */
private void previewMedia(boolean isImage) {
// Checking whether captured media is image or video
imgPreview.setVisibility(View.VISIBLE);
// bimatp factory
BitmapFactory.Options options = new BitmapFactory.Options();
// down sizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 8;
final Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
imgPreview.setImageBitmap(bitmap);
}
/**
* Uploading the file to server
* */
private class UploadFileToServer extends AsyncTask<Void, Integer, String> {
@Override
protected void onPreExecute() {
// setting progress bar to zero
progressBar.setProgress(0);
super.onPreExecute();
}
@Override
protected void onProgressUpdate(Integer... progress) {
// Making progress bar visible
progressBar.setVisibility(View.VISIBLE);
// updating progress bar value
progressBar.setProgress(progress[0]);
// updating percentage value
txtPercentage.setText(String.valueOf(progress[0]) + "%");
}
@Override
protected String doInBackground(Void... params) {
return uploadFile();
}
private String uploadFile() {
String responseString = null;
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(Config.FILE_UPLOAD_URL);
try {
MultipartEntity mpEntity = new MultipartEntity();
if (filePath !=null) {
File file = new File(filePath);
FileBody fb=new FileBody(file);
Log.d("EDIT USER PROFILE", "UPLOAD: file length = " + file.length());
Log.d("EDIT USER PROFILE", "UPLOAD: file exist = " + file.exists());
mpEntity.addPart("image", fb);
}
httppost.setEntity(mpEntity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity r_entity = response.getEntity();
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 200) {
// Server response
responseString = EntityUtils.toString(r_entity);
} else {
responseString = "Error occurred! Http Status Code: "
+ statusCode;
}
} catch (ClientProtocolException e) {
responseString = e.toString();
} catch (IOException e) {
responseString = e.toString();
}
return responseString;
}
@Override
protected void onPostExecute(String result) {
Log.e(TAG, "Response from server: " + result);
// showing the server response in an alert dialog
showAlert(result);
super.onPostExecute(result);
}
}
/**
* Method to show alert dialog
* */
private void showAlert(String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(message).setTitle("Response from Servers")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// do nothing
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
和fileUpload.php
<?php
// Path to move uploaded files
$target_path = "uploads/";
// array for final json response
$response = array();
// final file url that is being uploaded
$file_upload_url = 'http://opts.netne.net/uploads/';
if (isset($_FILES['image']['name'])) {
$target_path = $target_path . basename($_FILES['image']['name']);
$response['file_name'] = basename($_FILES['image']['name']);
try {
// Throws exception incase file is not being moved
if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
// make error flag true
$response['error'] = true;
$response['message'] = 'Could not move the file!';
}
// File successfully uploaded
$response['message'] = 'File uploaded successfully!';
$response['error'] = false;
$response['file_path'] = $file_upload_url . basename($_FILES['image']['name']);
} catch (Exception $e) {
// Exception occurred. Make error flag true
$response['error'] = true;
$response['message'] = $e->getMessage();
}
} else {
// File parameter is missing
$response['error'] = true;
$response['message'] = 'Not received any file!F';
}
// Echo final json response to client
//echo json_encode($response);
echo $target_path;
?>
我的应用在代码中使用MultiPartEntity时停止响应。请提供一个解决方案,将捕获的图像上传到服务器。
这是完整的logcat
04-11 15:41:20.523: I/clicks(9214): You Clicked B1
04-11 15:41:22.653: W/IInputConnectionWrapper(9214): showStatusIcon on inactive InputConnection
04-11 15:41:28.593: W/dalvikvm(9214): Unable to resolve superclass of Lorg/apache/http/entity/mime/content/AbstractContentBody; (2647)
04-11 15:41:28.593: W/dalvikvm(9214): Link of class 'Lorg/apache/http/entity/mime/content/AbstractContentBody;' failed
04-11 15:41:28.593: W/dalvikvm(9214): Unable to resolve superclass of Lorg/apache/http/entity/mime/content/FileBody; (2297)
04-11 15:41:28.593: W/dalvikvm(9214): Link of class 'Lorg/apache/http/entity/mime/content/FileBody;' failed
04-11 15:41:28.593: E/dalvikvm(9214): Could not find class 'org.apache.http.entity.mime.content.FileBody', referenced from method com.example.opts.UploadActivity$UploadFileToServer.uploadFile
04-11 15:41:28.593: W/dalvikvm(9214): VFY: unable to resolve new-instance 2299 (Lorg/apache/http/entity/mime/content/FileBody;) in Lcom/example/opts/UploadActivity$UploadFileToServer;
04-11 15:41:28.593: D/dalvikvm(9214): VFY: replacing opcode 0x22 at 0x0025
04-11 15:41:28.593: W/dalvikvm(9214): Unable to resolve superclass of Lorg/apache/http/entity/mime/content/AbstractContentBody; (2647)
04-11 15:41:28.593: W/dalvikvm(9214): Link of class 'Lorg/apache/http/entity/mime/content/AbstractContentBody;' failed
04-11 15:41:28.593: W/dalvikvm(9214): Unable to resolve superclass of Lorg/apache/http/entity/mime/content/FileBody; (2297)
04-11 15:41:28.593: W/dalvikvm(9214): Link of class 'Lorg/apache/http/entity/mime/content/FileBody;' failed
04-11 15:41:28.593: D/dalvikvm(9214): DexOpt: unable to opt direct call 0x4291 at 0x27 in Lcom/example/opts/UploadActivity$UploadFileToServer;.uploadFile
04-11 15:41:28.593: W/dalvikvm(9214): Unable to resolve superclass of Lorg/apache/http/entity/mime/HttpMultipart; (2646)
04-11 15:41:28.593: W/dalvikvm(9214): Link of class 'Lorg/apache/http/entity/mime/HttpMultipart;' failed
04-11 15:41:28.593: E/dalvikvm(9214): Could not find class 'org.apache.http.entity.mime.HttpMultipart', referenced from method org.apache.http.entity.mime.MultipartEntity.<init>
04-11 15:41:28.593: W/dalvikvm(9214): VFY: unable to resolve new-instance 2291 (Lorg/apache/http/entity/mime/HttpMultipart;) in Lorg/apache/http/entity/mime/MultipartEntity;
04-11 15:41:28.593: D/dalvikvm(9214): VFY: replacing opcode 0x22 at 0x0003
04-11 15:41:28.593: I/dalvikvm(9214): Failed resolving Lorg/apache/http/entity/mime/content/ContentBody; interface 2640 'Lorg/apache/james/mime4j/message/Body;'
04-11 15:41:28.593: W/dalvikvm(9214): Link of class 'Lorg/apache/http/entity/mime/content/ContentBody;' failed
04-11 15:41:28.593: W/dalvikvm(9214): VFY: unable to find class referenced in signature (Lorg/apache/http/entity/mime/content/ContentBody;)
04-11 15:41:28.593: W/dalvikvm(9214): Unable to resolve superclass of Lorg/apache/http/entity/mime/HttpMultipart; (2646)
04-11 15:41:28.593: W/dalvikvm(9214): Link of class 'Lorg/apache/http/entity/mime/HttpMultipart;' failed
04-11 15:41:28.593: W/dalvikvm(9214): Unable to resolve superclass of Lorg/apache/http/entity/mime/FormBodyPart; (2641)
04-11 15:41:28.593: W/dalvikvm(9214): Link of class 'Lorg/apache/http/entity/mime/FormBodyPart;' failed
04-11 15:41:28.593: E/dalvikvm(9214): Could not find class 'org.apache.http.entity.mime.FormBodyPart', referenced from method org.apache.http.entity.mime.MultipartEntity.addPart
04-11 15:41:28.593: W/dalvikvm(9214): VFY: unable to resolve new-instance 2289 (Lorg/apache/http/entity/mime/FormBodyPart;) in Lorg/apache/http/entity/mime/MultipartEntity;
04-11 15:41:28.593: D/dalvikvm(9214): VFY: replacing opcode 0x22 at 0x0002
04-11 15:41:28.593: W/dalvikvm(9214): Unable to resolve superclass of Lorg/apache/http/entity/mime/HttpMultipart; (2646)
04-11 15:41:28.593: W/dalvikvm(9214): Link of class 'Lorg/apache/http/entity/mime/HttpMultipart;' failed
04-11 15:41:28.593: W/dalvikvm(9214): Unable to resolve superclass of Lorg/apache/http/entity/mime/HttpMultipart; (2646)
04-11 15:41:28.593: W/dalvikvm(9214): Link of class 'Lorg/apache/http/entity/mime/HttpMultipart;' failed
04-11 15:41:28.593: I/dalvikvm(9214): Could not find method org.apache.http.entity.mime.HttpMultipart.getTotalLength, referenced from method org.apache.http.entity.mime.MultipartEntity.getContentLength
04-11 15:41:28.593: W/dalvikvm(9214): VFY: unable to resolve virtual method 16997: Lorg/apache/http/entity/mime/HttpMultipart;.getTotalLength ()J
04-11 15:41:28.593: D/dalvikvm(9214): VFY: replacing opcode 0x6e at 0x0006
04-11 15:41:28.593: W/dalvikvm(9214): Unable to resolve superclass of Lorg/apache/http/entity/mime/HttpMultipart; (2646)
04-11 15:41:28.593: W/dalvikvm(9214): Link of class 'Lorg/apache/http/entity/mime/HttpMultipart;' failed
04-11 15:41:28.603: W/dalvikvm(9214): Unable to resolve superclass of Lorg/apache/http/entity/mime/HttpMultipart; (2646)
04-11 15:41:28.603: W/dalvikvm(9214): Link of class 'Lorg/apache/http/entity/mime/HttpMultipart;' failed
04-11 15:41:28.603: I/dalvikvm(9214): Could not find method org.apache.http.entity.mime.HttpMultipart.getBodyParts, referenced from method org.apache.http.entity.mime.MultipartEntity.isRepeatable
04-11 15:41:28.613: W/dalvikvm(9214): VFY: unable to resolve virtual method 16990: Lorg/apache/http/entity/mime/HttpMultipart;.getBodyParts ()Ljava/util/List;
04-11 15:41:28.623: D/dalvikvm(9214): VFY: replacing opcode 0x6e at 0x0002
04-11 15:41:28.623: W/dalvikvm(9214): Unable to resolve superclass of Lorg/apache/http/entity/mime/HttpMultipart; (2646)
04-11 15:41:28.623: W/dalvikvm(9214): Link of class 'Lorg/apache/http/entity/mime/HttpMultipart;' failed
04-11 15:41:28.623: W/dalvikvm(9214): Unable to resolve superclass of Lorg/apache/http/entity/mime/HttpMultipart; (2646)
04-11 15:41:28.623: W/dalvikvm(9214): Link of class 'Lorg/apache/http/entity/mime/HttpMultipart;' failed
04-11 15:41:28.623: I/dalvikvm(9214): Could not find method org.apache.http.entity.mime.HttpMultipart.writeTo, referenced from method org.apache.http.entity.mime.MultipartEntity.writeTo
04-11 15:41:28.623: W/dalvikvm(9214): VFY: unable to resolve virtual method 17002: Lorg/apache/http/entity/mime/HttpMultipart;.writeTo (Ljava/io/OutputStream;)V
04-11 15:41:28.623: D/dalvikvm(9214): VFY: replacing opcode 0x6e at 0x0002
04-11 15:41:28.623: W/dalvikvm(9214): Unable to resolve superclass of Lorg/apache/http/entity/mime/HttpMultipart; (2646)
04-11 15:41:28.623: W/dalvikvm(9214): Link of class 'Lorg/apache/http/entity/mime/HttpMultipart;' failed
04-11 15:41:28.623: D/dalvikvm(9214): DexOpt: unable to opt direct call 0x425a at 0x07 in Lorg/apache/http/entity/mime/MultipartEntity;.<init>
04-11 15:41:28.623: D/dalvikvm(9214): DexOpt: unable to opt direct call 0x4cbb at 0x1e in Lorg/apache/http/entity/mime/MultipartEntity;.<init>
04-11 15:41:28.623: D/dalvikvm(9214): DexOpt: unable to opt direct call 0x4cb7 at 0x25 in Lorg/apache/http/entity/mime/MultipartEntity;.<init>
04-11 15:41:28.623: W/dalvikvm(9214): Unable to resolve superclass of Lorg/apache/http/entity/mime/FormBodyPart; (2641)
04-11 15:41:28.623: W/dalvikvm(9214): Link of class 'Lorg/apache/http/entity/mime/FormBodyPart;' failed
04-11 15:41:28.623: D/dalvikvm(9214): DexOpt: unable to opt direct call 0x424e at 0x04 in Lorg/apache/http/entity/mime/MultipartEntity;.addPart
04-11 15:41:28.623: W/dalvikvm(9214): threadid=12: thread exiting with uncaught exception (group=0x95d33b20)
04-11 15:41:28.623: D/AndroidRuntime(9214): procName from cmdline: com.example.opts
04-11 15:41:28.623: E/AndroidRuntime(9214): in writeCrashedAppName, pkgName :com.example.opts
04-11 15:41:28.623: D/AndroidRuntime(9214): file written successfully with content: com.example.opts StringBuffer : ;com.example.opts
04-11 15:41:28.653: I/Process(9214): Sending signal. PID: 9214 SIG: 9
04-11 15:41:28.653: E/AndroidRuntime(9214): FATAL EXCEPTION: AsyncTask #1
04-11 15:41:28.653: E/AndroidRuntime(9214): Process: com.example.opts, PID: 9214
04-11 15:41:28.653: E/AndroidRuntime(9214): java.lang.RuntimeException: An error occured while executing doInBackground()
04-11 15:41:28.653: E/AndroidRuntime(9214): at android.os.AsyncTask$3.done(AsyncTask.java:300)
04-11 15:41:28.653: E/AndroidRuntime(9214): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
04-11 15:41:28.653: E/AndroidRuntime(9214): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
04-11 15:41:28.653: E/AndroidRuntime(9214): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
04-11 15:41:28.653: E/AndroidRuntime(9214): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-11 15:41:28.653: E/AndroidRuntime(9214): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-11 15:41:28.653: E/AndroidRuntime(9214): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-11 15:41:28.653: E/AndroidRuntime(9214): at java.lang.Thread.run(Thread.java:841)
04-11 15:41:28.653: E/AndroidRuntime(9214): Caused by: java.lang.NoClassDefFoundError: org.apache.http.entity.mime.HttpMultipart
04-11 15:41:28.653: E/AndroidRuntime(9214): at org.apache.http.entity.mime.MultipartEntity.<init>(MultipartEntity.java:77)
04-11 15:41:28.653: E/AndroidRuntime(9214): at org.apache.http.entity.mime.MultipartEntity.<init>(MultipartEntity.java:100)
04-11 15:41:28.653: E/AndroidRuntime(9214): at com.example.opts.UploadActivity$UploadFileToServer.uploadFile(UploadActivity.java:158)
04-11 15:41:28.653: E/AndroidRuntime(9214): at com.example.opts.UploadActivity$UploadFileToServer.doInBackground(UploadActivity.java:139)
04-11 15:41:28.653: E/AndroidRuntime(9214): at com.example.opts.UploadActivity$UploadFileToServer.doInBackground(UploadActivity.java:1)
04-11 15:41:28.653: E/AndroidRuntime(9214): at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-11 15:41:28.653: E/AndroidRuntime(9214): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-11 15:41:28.653: E/AndroidRuntime(9214): ... 4 more