我google了很多,但它不工作。我发现了很多网站的信息,但我的应用程序崩溃的所有网站。我想打开的图片是:“lastfile.png”。它存储在内部存储器中,所以我用openFileInput(“lastfile.png”)打开它;上传图片到服务器
我在AsyncTask中完成它。
这是到目前为止我的代码:
class PostTask extends AsyncTask<String, String, String>{
@Override
protected String doInBackground(String... uri) {
if(picture == null) {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;
String responseString = null;
try {
response = httpclient.execute(new HttpGet(uri[0]));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
} else{
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
} catch (ClientProtocolException e) {
Toast.makeText(AddStoryActivity.this, getResources().getString(R.string.error), Toast.LENGTH_LONG).show();
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(AddStoryActivity.this, getResources().getString(R.string.error), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
return responseString;
} else {
/* IMAGE UPLOAD */
}
return "";
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
progress.cancel();
Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
}
}
哪里图像数据上传?这似乎更像是从服务器上卸载某些东西的方式。 对于图片上传,您应该创建一个HttpPost请求,并在您调用'httpClient.execute'之前,将要上传的文件附加到它。 –