我想将图片从图库上传到服务器并发送image_url以保留在数据库中以检索它们以便在android中再次显示。顺便说一句,我不知道如何启动它。
现在我开始尝试应用此处的代码:https://trinitytuts.com/capture-image-upload-server-android/
这是我的新代码:
package com.theclubprogramming.clickanduploadpic;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.ArrayList;
public class MyActivity extends Activity {
Button btpic, btnup;
private Uri fileUri;
String picturePath;
//File pictureFile;
Uri selectedImage;
Bitmap photo;
String ba1;
public static String URL = "http://192.168.1.39/picUp/index.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
btpic = (Button) findViewById(R.id.cpic);
btpic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
clickpic();
}
});
btnup = (Button) findViewById(R.id.up);
btnup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
upload();
}
});
}
private void upload() {
// Image location URL
Log.e("path", "----------------" + picturePath);
// Image
Bitmap bm = BitmapFactory.decodeFile(picturePath);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte[] ba = bao.toByteArray();
ba1 = Base64.encodeBytes(ba);
Log.e("base64", "-----" + ba1);
// Upload image to server
new uploadToServer().execute();
}
private void clickpic() {
// Check Camera
if (getApplicationContext().getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA)) {
// Open default camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, 100);
} else {
Toast.makeText(getApplication(), "Camera not supported", Toast.LENGTH_LONG).show();
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 100 && resultCode == RESULT_OK) {
selectedImage = data.getData();
photo = (Bitmap) data.getExtras().get("data");
// Cursor to get image uri to display
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
//fix
//pictureFile = new File(cursor.getString(columnIndex));
//picturePath = pictureFile.getAbsolutePath();
//cursor.close();
picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView imageView = (ImageView) findViewById(R.id.Imageprev);
imageView.setImageBitmap(photo);
}
}
public class uploadToServer extends AsyncTask<Void, Void, String> {
private ProgressDialog pd = new ProgressDialog(MyActivity.this);
protected void onPreExecute() {
super.onPreExecute();
pd.setMessage("Wait image uploading!");
pd.show();
}
@Override
protected String doInBackground(Void... params) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("base64", ba1));
nameValuePairs.add(new BasicNameValuePair("ImageName", System.currentTimeMillis() + ".jpg"));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String st = EntityUtils.toString(response.getEntity());
Log.v("log_tag", "In the try Loop" + st);
} catch (Exception e) {
Log.v("log_tag", "Error in http connection " + e.toString());
}
return "Success";
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
pd.hide();
pd.dismiss();
}
}
}
这是logcat错误:
12-28 08:05:12.171 2422-2422/com.theclubprogramming.clickanduploadpic E/path: ----------------null
12-28 08:05:12.171 2422-2422/com.theclubprogramming.clickanduploadpic E/BitmapFactory: Unable to decode stream: java.lang.NullPointerException
12-28 08:05:12.175 2422-2422/com.theclubprogramming.clickanduploadpic D/AndroidRuntime: Shutting down VM
12-28 08:05:12.175 2422-2422/com.theclubprogramming.clickanduploadpic W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xa619e908)
12-28 08:05:12.183 2422-2422/com.theclubprogramming.clickanduploadpic E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.theclubprogramming.clickanduploadpic.MyActivity.upload(MyActivity.java:73)
at com.theclubprogramming.clickanduploadpic.MyActivity.access$100(MyActivity.java:34)
at com.theclubprogramming.clickanduploadpic.MyActivity$2.onClick(MyActivity.java:61)
at android.view.View.performClick(View.java:4204)
at android.view.View$PerformClick.run(View.java:17355)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
12-28 08:05:12.191 2422-2426/com.theclubprogramming.clickanduploadpic D/dalvikvm: GC_CONCURRENT freed 101K, 8% free 2644K/2860K, paused 2ms+0ms, total 8ms
12-28 08:05:15.203 2422-2422/com.theclubprogramming.clickanduploadpic I/Process: Sending signal. PID: 2422 SIG: 9
你能告诉我如何解决这个问题吗?谢谢你的关注。