【发布时间】:2015-07-07 07:51:12
【问题描述】:
不幸的是,当我尝试将一些图像从 android 设备上传到数据库时,我遇到了一些问题。 图像在一个文件夹中。此文件夹包含图像以及其他内容。我不知道图片的名称,我只需要上传图片(jpg)。在我上传图片之前,我需要使用 base64 对它们进行编码。
首先我从文件夹中获取 jpg 文件。然后我从图像名称中获取 ID。之后我通过base64对其进行编码:
Button upload = (Button) findViewById(R.id.upload);
upload.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
String path = Environment.getExternalStorageDirectory().getPath();
File dir = new File(path);
File[] files = dir.listFiles();
for (int i = 0; i < files.length; ++i) {
if (files[i].getName().endsWith(".jpg")) {
pics = new File(String.valueOf(files[i]));
id = String.valueOf(files[i]);
String sub = id.substring(id.lastIndexOf("/") + 1);
int index = sub.indexOf("_");
String book;
if (index >= 0) {
book = sub.substring(0, index);
ID = book;
Log.e("ID", ID);
}
Bitmap imagex = BitmapFactory.decodeFile(pics.getAbsolutePath());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imagex.compress(Bitmap.CompressFormat.JPEG, 70, baos);
byte[] b = baos.toByteArray();
Image = Base64.encodeToString(b, Base64.DEFAULT);
try {
new HttpAsyncTask(ID,Image,Nummer).execute("https://....");
} catch (Exception e) {
Log.e("InputStream", e.getMessage());
}
Log.e("PICS", id);
}
}
}
});
public String POST(String url) {
InputStream inputStream;
try {
HttpClient httpclient = classxy.getNewHttpClient();
HttpPost httpPost = new HttpPost(url);
String json = "";
JSONObject jsonObject = new JSONObject();
jsonObject.put("bookId", ID);
jsonObject.put("imageString", Image);
jsonObject.put("imageNumber", Nummer);
json = jsonObject.toString();
StringEntity se = new StringEntity(json);
httpPost.setEntity(se);
httpPost.setHeader("Apikey", data);
httpPost.setHeader("Modul", "upload_image");
HttpResponse httpResponse = httpclient.execute(httpPost);
inputStream = httpResponse.getEntity().getContent();
if (inputStream != null)
result = classxy.convertInputStreamToString(inputStream);
else
result = "Fehler!";
} catch (Exception e) {
Log.e("InputStream", e.getLocalizedMessage());
}
int num = Integer.parseInt(Nummer);
num++;
Nummer = Integer.toString(num);
return result;
}
public class HttpAsyncTask extends AsyncTask<String, Void, String> {
private final Object ID, Image, Nummer;
public HttpAsyncTask(Object ID, Object Image, Object Nummer) {
this.ID = ID;
this.Image = Image;
this.Nummer = Nummer;
}
protected String doInBackground(String... urls) {
return POST(urls[0]);
}
protected void onPostExecute(String result) {
if (result.matches("(.*)false(.*)")) {
Toast.makeText(getApplicationContext(), "....", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "...", Toast.LENGTH_SHORT).show();
}
Log.e("RESPONSE", result);
}
}
它确实通过 base64 对图像进行编码,并且确实上传了一些图像。不幸的是,它只上传第一张图片或多次上传一张图片。它永远不会以正确的顺序上传正确数量的图像。我已经在这个问题上坐了一段时间,无法弄清楚我做错了什么。
你能告诉我我做错了什么吗?
【问题讨论】:
-
我强烈建议您使用类似 okhttp 的东西来执行 http 请求。 github.com/square/okhttp/wiki/Recipes
-
我同意万南的观点。除了总体上不是很好之外,HttpClient 在较新的 Android 版本中已被弃用。
-
它应该如何工作: 1. 我从文件夹中读取了第一张图片。 2. 从文件名中获取 ID 3. 使用 base64 编码图像 4. 将第一个(编码的)图像发送到服务器。 5. 接收服务器结果。然后继续下一张图片,依此类推...
标签: java android android-asynctask base64