美好的一天。请有人帮我这个吗? 是否可以将图像从android上传到远程数据库? 我能够与远程数据库通信并执行简单的操作,如从数据库读取,写入和删除,但在上传图像时遇到问题。我花了一整天的时间来搜索和阅读方法,以便从stackoverflow和其他网站完成此任务。 这就是我试过的
public class MainActivity extends Activity {
Bitmap bitmap1;
byte[]image1byte;
...
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == PICK_IMAGE_1) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
bitmap1 = BitmapFactory.decodeFile(picturePath);
image1 = (ImageView) findViewById(R.id.imageView1);
image1.setImageBitmap(bitmap1);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap1.compress(Bitmap.CompressFormat.JPEG,100,stream);
image1byte=stream.toByteArray();
}
我的AsyncTask任务是;
class CreateNewMessage extends AsyncTask<String,String,String>{
@Override
protected void onPreExecute(){
pDialog=new ProgressDialog(MainActivity.this);
pDialog.setMessage("Uploading Message...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... strings) {
String topic=inputTopic.getText().toString();
String message=inputMessage.getText().toString();
String other=inputOther.getText().toString();
List<NameValuePair>params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("topic",topic));
params.add(new BasicNameValuePair("message",message));
params.add(new BasicNameValuePair("details",details));
params.add(new BasicNameValuePair("image",sendImage1));
JSONObject json=jsonParser.makeHttpRequest(url_create_message,"GET",params);
try {
response=json.getString(TAG_SUCCESS);
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("Response",json.toString());
return response;
}
protected void onPostExecute(String response) {
Toast.makeText(getBaseContext(),"Finished..Response= "+response,Toast.LENGTH_LONG).show();
pDialog.dismiss();
}
}
但是我收到以下错误;
12-12 16:22:47.017 15942-15985/com.example.mcleroy.studentboxadminpanel E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #3
Process: com.example.mcleroy.studentboxadminpanel, PID: 15942
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at com.example.mcleroy.studentboxadminpanel.MainActivity$CreateNewMessage.doInBackground(MainActivity.java:119)
at com.example.mcleroy.studentboxadminpanel.MainActivity$CreateNewMessage.doInBackground(MainActivity.java:96)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587) 在java.lang.Thread.run(Thread.java:841) 12-12 16:22:48.286 15942-15942 / com.example.mcleroy.studentboxadminpanel E / WindowManager:android.view.WindowLeaked:Activity com.example.mcleroy.studentboxadminpanel.MainActivity泄露了窗口com.android.internal.policy。最初添加的impl.PhoneWindow $ DecorView {4261d878 VE .... R ...... D 0,0-684,192} 在android.view.ViewRootImpl。(ViewRootImpl.java:376) 在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248) 在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69) 在android.app.Dialog.show(Dialog.java:286) at com.example.mcleroy.studentboxadminpanel.MainActivity $ CreateNewMessage.onPreExecute(MainActivity.java:104) 在android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587) 在android.os.AsyncTask.execute(AsyncTask.java:535) 在com.example.mcleroy.studentboxadminpanel.MainActivity.createMessage(MainActivity.java:94) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:515) 在android.view.View $ 1.onClick(View.java:3846) 在android.view.View.performClick(View.java:4466) 在android.view.View $ PerformClick.run(View.java:18537) 在android.os.Handler.handleCallback(Handler.java:733) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:136) 在android.app.ActivityThread.main(ActivityThread.java:5102) at java.lang.reflect.Method.invokeNative(Native Method) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:785) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132) 在dalvik.system.NativeStart.main(本地方法)
请请任何人查看我的代码并告诉我我没有写的内容,因为我真的需要能够将我的应用程序中的图像上传和下载到远程数据库,分别从远程数据库上传到应用程序。还是不可能做到这一点?
这是我的PHP脚本
<?php
$servername = "xxxxx";
$username = "xxxxx";
$password = "xxxxx";
$dbname = "xxxxx";
$response = array();
$topic = $_GET['name'];
$message = $_GET['price'];
$details = $_GET['description'];
$image=$_GET['image'];
$con = mysql_connect($servername,$username,$password) or die(mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$result = mysql_query("INSERT INTO messages(topic, messages, others,image) VALUES('$name', '$price',`` '$description','$image')");
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Message successfully created.";
// echoing JSON response
echo json_encode($response);
}
&GT;
在您尝试提供帮助时,请提前致谢;