所以我试图将带有附加信息的图像上传到网络服务器,但是当我要上传时我会收到错误。
我的日志猫
12-14 00:17:25.246 11931-11931 / com.erlawkward.albarol.capstoneeventlocator D / ViewRootImpl:ViewPostImeInputStage ACTION_DOWN 12-14 00:17:25.346 11931-11931 / com.erlawkward.albarol.capstoneeventlocator E / ViewRootImpl:sendUserActionEvent()mView == null 12-14 00:17:26.116 11931-11931 / com.erlawkward.albarol.capstoneeventlocator D / ViewRootImpl:ViewPostImeInputStage ACTION_DOWN 12-14 00:17:26.256 11931-16240 / com.erlawkward.albarol.capstoneeventlocator E / AndroidRuntime:FATAL EXCEPTION:AsyncTask#1 过程:com.erlawkward.albarol.capstoneeventlocator,PID:11931 java.lang.RuntimeException:执行时发生错误 doInBackground() 在android.os.AsyncTask $ 3.done(AsyncTask.java:300) 在 java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 在java.util.concurrent.FutureTask.run(FutureTask.java:242) 在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587) 在java.lang.Thread.run(Thread.java:818) 引起:java.lang.ArrayIndexOutOfBoundsException:length = 0;索引= 0 在 com.erlawkward.albarol.capstoneeventlocator.CreateEvent_2Activity $ 1UploadImage.doInBackground(CreateEvent_2Activity.java:268) 在 com.erlawkward.albarol.capstoneeventlocator.CreateEvent_2Activity $ 1UploadImage.doInBackground(CreateEvent_2Activity.java:248) 在android.os.AsyncTask $ 2.call(AsyncTask.java:288) 在java.util.concurrent.FutureTask.run(FutureTask.java:237) 在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 在 java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:587) 在java.lang.Thread.run(Thread.java:818)12-14 00:17:26.626 11931-12007 / com.erlawkward.albarol.capstoneeventlocator D / OpenGLRenderer:0x9bc0c800上的endAllStagingAnimators (RippleDrawable),句柄为0x9df86430 12-14 00:17:26.636 11931-11931 / com.erlawkward.albarol.capstoneeventlocator I / Timeline: 时间轴:Activity_idle id:android.os.BinderProxy@2322f347 时间:47950722
我的代码
private int PICK_IMAGE_REQUEST = 1;
private Button b_choose;
private Button b_upload;
private Button b_viewimage;
private Button time_button;
private Button date_button;
private ImageView iv_image;
private Bitmap bitmap;
private Uri filePath;
private TextView time_textview;
private TextView date_textview;
private TextView timeTextView;
private TextView dateTextView;
String date;
String time;
Intent i = getIntent();
String title;
String address;
String description;
String phone;
String lng;
String lat;
String category;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_event_2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
b_choose = (Button) findViewById(R.id.b_choose);
b_upload = (Button) findViewById(R.id.b_upload);
/*b_viewimage = (Button) findViewById(R.id.b_viewimage);*/
/*Date and Time Picker*/
timeTextView = (TextView)findViewById(R.id.time_textview);
dateTextView = (TextView)findViewById(R.id.date_textview);
Button timeButton = (Button)findViewById(R.id.time_button);
Button dateButton = (Button)findViewById(R.id.date_button);
iv_image = (ImageView) findViewById(R.id.iv_image);
time_textview = (TextView) findViewById(R.id.time_textview);
date_textview = (TextView) findViewById(R.id.date_textview);
b_choose.setOnClickListener(this);
b_upload.setOnClickListener(this);
timeButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar now = Calendar.getInstance();
TimePickerDialog tpd = TimePickerDialog.newInstance(
CreateEvent_2Activity.this,
now.get(Calendar.HOUR_OF_DAY),
now.get(Calendar.MINUTE),
false
);
tpd.setThemeDark(true);
tpd.setTitle("TimePicker Title");
tpd.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialogInterface) {
Log.d("TimePicker", "Dialog was cancelled");
}
});
tpd.show(getFragmentManager(), "Timepickerdialog");
}
});
dateButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar now = Calendar.getInstance();
DatePickerDialog dpd = DatePickerDialog.newInstance(
CreateEvent_2Activity.this,
now.get(Calendar.YEAR),
now.get(Calendar.MONTH),
now.get(Calendar.DAY_OF_MONTH)
);
dpd.setThemeDark(true);
dpd.showYearPickerFirst(true);
dpd.setTitle("DatePicker Title");
dpd.show(getFragmentManager(), "Datepickerdialog");
}
});
}
@Override
public void onResume() {
super.onResume();
TimePickerDialog tpd = (TimePickerDialog) getFragmentManager().findFragmentByTag("Timepickerdialog");
DatePickerDialog dpd = (DatePickerDialog) getFragmentManager().findFragmentByTag("Datepickerdialog");
if(tpd != null) tpd.setOnTimeSetListener(this);
if(dpd != null) dpd.setOnDateSetListener(this);
}
@Override
public void onClick(View v) {
if (v == b_choose) {
showFileChooser();
}
if(v == b_upload){
Intent i = getIntent();
title = i.getStringExtra("title");
address = i.getStringExtra("address");
time = date_textview.getText().toString();
date = time_textview.getText().toString();
phone = i.getStringExtra("phone");
description = i.getStringExtra("description");
lng = i.getStringExtra("lng");
lat = i.getStringExtra("lat");
category = i.getStringExtra("category");
uploadImage(title,address,time,date,phone,description,lng,lat,category);
}
}
@Override
public void onTimeSet(RadialPickerLayout view, int hourOfDay, int minute, int second) {
String hourString = hourOfDay < 10 ? "0"+hourOfDay : ""+hourOfDay;
String minuteString = minute < 10 ? "0"+minute : ""+minute;
String time = hourString+"h"+minuteString+"m";
timeTextView.setText(time);
}
@Override
public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) {
String date = +dayOfMonth+"/"+(++monthOfYear)+"/"+year;
dateTextView.setText(date);
}
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
iv_image.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
public String getStringImage(Bitmap bmp){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
private void uploadImage(String title, String address, String time, String date, String phone, String description, String lng, String lat, String category) {
class UploadImage extends AsyncTask<Bitmap, Void, String> {
ProgressDialog loading;
ServerConnection rh = new ServerConnection();
@Override
protected void onPreExecute() {
super.onPreExecute();
loading = ProgressDialog.show(CreateEvent_2Activity.this, "Uploading Image", "Please wait...", true, true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
loading.dismiss();
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(Bitmap... params) {
Bitmap bitmap = params[0];
String uploadImage = getStringImage(bitmap);
HashMap<String, String> data = new HashMap<>();
data.put(UPLOAD_KEY, uploadImage);
data.put("name", CreateEvent_2Activity.this.title);
data.put("address", CreateEvent_2Activity.this.description);
data.put("time", CreateEvent_2Activity.this.time);
data.put("date", CreateEvent_2Activity.this.date);
data.put("phone", CreateEvent_2Activity.this.phone);
data.put("description", CreateEvent_2Activity.this.description);
data.put("lat", CreateEvent_2Activity.this.lat);
data.put("lng", CreateEvent_2Activity.this.lng);
data.put("categoryType", CreateEvent_2Activity.this.category);
String result = rh.sendPostRequest(UPLOAD_URL, data);
return result;
}
}
UploadImage up = new UploadImage();
up.execute();
}
}
我的doInbackground中的代码是否正确? 即时通讯新手Android编程我还在学习如何。