我正在开发一个应用程序,我必须将照片发送到 php 服务器,为此我尝试了以下代码
public class UploadImage extends Activity {
Uri currImageURI;
String image_name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button upload_btn = (Button) this.findViewById(R.id.upload);
upload_btn.setOnClickListener( new OnClickListener(){
@Override
public void onClick(View view) {
// To open up a gallery browser
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),1);
}
});
HttpUploader uploader = new HttpUploader();
try {
image_name = uploader.execute(getRealPathFromURI(currImageURI)).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
// currImageURI is the global variable I’m using to hold the content:
currImageURI = data.getData();
System.out.println("Current image Path is ----->" + getRealPathFromURI(currImageURI));
TextView tv_path = (TextView) findViewById(R.id.textView1);
tv_path.setText(getRealPathFromURI(currImageURI));
}
}
}
// Convert the image URI to the direct file system path of the image file
public String getRealPathFromURI(Uri contentUri) {
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
// 上传者类
class HttpUploader extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... path) {
// TODO Auto-generated method stub
String outPut = null;
for (String sdPath : path) {
Bitmap bitmapOrg = BitmapFactory.decodeFile(sdPath);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
// Resize the image
double width = bitmapOrg.getWidth();
double height = bitmapOrg.getHeight();
double ratio = 400/width;
int newheight = (int)(ratio*height);
System.out.println("----width" + width);
System.out.println("----height" + height);
bitmapOrg = Bitmap.createScaledBitmap(bitmapOrg, 400, newheight, true);
// Here you can define .PNG as well
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 95, bao);
byte[] ba = bao.toByteArray();
String ba1 = Base64.encodeBytes(ba);
System.out.println("uploading image now ---" + ba1);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image", ba1));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.aamirarif.com/organizer_json/mobile/test/UploadToServer.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
// print response
outPut = EntityUtils.toString(entity);
Log.i("GET RESPONSE---", outPut);
//is = entity.getContent();
Log.e("log_tag ******", "good connection");
bitmapOrg.recycle();
} catch (Exception e) {
Log.e("log_tag ******", "Error in http connection " + e.toString());
}
}
return outPut;
}
}
但在这段代码中,我得到了以下异常 1
0-14 02:59:59.808: W/dalvikvm(2670): threadid=1: thread exiting with uncaught exception (group=0x41465700)
10-14 02:59:59.817: E/AndroidRuntime(2670): FATAL EXCEPTION: main
10-14 02:59:59.817: E/AndroidRuntime(2670): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testing.imageposting/com.testing.imageposting.UploadImage}: java.lang.NullPointerException
10-14 02:59:59.817: E/AndroidRuntime(2670): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
10-14 02:59:59.817: E/AndroidRuntime(2670): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
10-14 02:59:59.817: E/AndroidRuntime(2670): at android.app.ActivityThread.access$600(ActivityThread.java:141)
10-14 02:59:59.817: E/AndroidRuntime(2670): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
10-14 02:59:59.817: E/AndroidRuntime(2670): at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 02:59:59.817: E/AndroidRuntime(2670): at android.os.Looper.loop(Looper.java:137)
10-14 02:59:59.817: E/AndroidRuntime(2670): at android.app.ActivityThread.main(ActivityThread.java:5103)
10-14 02:59:59.817: E/AndroidRuntime(2670): at java.lang.reflect.Method.invokeNative(Native Method)
10-14 02:59:59.817: E/AndroidRuntime(2670): at java.lang.reflect.Method.invoke(Method.java:525)
10-14 02:59:59.817: E/AndroidRuntime(2670): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-14 02:59:59.817: E/AndroidRuntime(2670): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-14 02:59:59.817: E/AndroidRuntime(2670): at dalvik.system.NativeStart.main(Native Method)
10-14 02:59:59.817: E/AndroidRuntime(2670): Caused by: java.lang.NullPointerException
10-14 02:59:59.817: E/AndroidRuntime(2670): at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1144)
10-14 02:59:59.817: E/AndroidRuntime(2670): at android.content.ContentResolver.query(ContentResolver.java:398)
10-14 02:59:59.817: E/AndroidRuntime(2670): at android.content.ContentResolver.query(ContentResolver.java:357)
10-14 02:59:59.817: E/AndroidRuntime(2670): at com.testing.imageposting.UploadImage.getRealPathFromURI(UploadImage.java:78)
10-14 02:59:59.817: E/AndroidRuntime(2670): at com.testing.imageposting.UploadImage.onCreate(UploadImage.java:54)
10-14 02:59:59.817: E/AndroidRuntime(2670): at android.app.Activity.performCreate(Activity.java:5133)
10-14 02:59:59.817: E/AndroidRuntime(2670): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
10-14 02:59:59.817: E/AndroidRuntime(2670): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
10-14 02:59:59.817: E/AndroidRuntime(2670): ... 11 more
因为我第一次将图像从 android 发布到 php 服务器,所以我处理起来有点复杂