我想通过将图像编码为base64-String将图像上传到我的数据库。
当我有图像并按下“上传”按钮时,我的代码会这样做。
private void upLoadPicture()
{
class UploadImage extends AsyncTask<Bitmap,Void,String>
{
//ProgressDialog loading;
@Override
protected String doInBackground(Bitmap... args) {
RequestHandler rh = new RequestHandler();
Bitmap tempBitmap = args[0];
String tempImageString = ConvertBitMapToString(tempBitmap);
HashMap<String,String> map = new HashMap<String,String>();
map.put(UPLOAD_KEY, tempImageString);
rh.SendPostRequest(DATABASE_REQUEST_PATH, map);
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
// loading = ProgressDialog.show(getApplicationContext(), "Uploading Image", "Please wait", true,true);
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
// loading.dismiss();
}
}
UploadImage Upi = new UploadImage();
Upi.execute(bitmap);
}
以下是方法和类
/*We store the picture in the database as a encoded 64-Base String so we need to encode it first*/
protected String ConvertBitMapToString(Bitmap bm)
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG,100,baos);
byte[] b = baos.toByteArray();
String tempString = Base64.encodeToString(b,Base64.DEFAULT);
return tempString;
}
我的班级应该处理上传
public class RequestHandler {
public void SendPostRequest(String url, HashMap<String,String> map)
{
String tempMessage,a = "";
HashMap<String,String> ImageHashmap = map;
try {
URL sendmeurl = new URL(url);
HttpURLConnection con = (HttpURLConnection) sendmeurl.openConnection();
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
OutputStream os = con.getOutputStream();
InputStream is = con.getInputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
bw.write(GetPOSTURL(ImageHashmap));
bw.flush();
bw.close();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while ((tempMessage = br.readLine()) != null){
a = tempMessage;
}
Log.d("recieve", a);
con.disconnect();
} catch (Exception e){
e.printStackTrace();
}
}
private String GetPOSTURL(HashMap<String,String> MapPOST)
{
StringBuilder result = new StringBuilder();
boolean first = true;
for (Map.Entry<String, String> entry : MapPOST.entrySet()) {
if (first)
first = false;
else
result.append("&");
try {
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
} catch (Exception e){
e.printStackTrace();
}
}
return result.toString();
}
在这个类中,我收到一个错误,我的BufferedWriter在执行方法GetPOSTUrl()
之前关闭了,我不知道为什么。我不确定如何深入研究这个问题。
修改
以下是我收到位图的方法
@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) {
filepath = data.getData();
try {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},FETCH_PICTURE_REQUEST);
} catch (Exception e){
e.printStackTrace();
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case FETCH_PICTURE_REQUEST:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED){
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filepath);
_IV.setImageBitmap(bitmap);
} catch(Exception e){
e.printStackTrace();
}
} else {
Toast.makeText(getApplicationContext(), "No Permission", Toast.LENGTH_SHORT).show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
Bitmap就在我的班级里面
private Uri filepath;
private Bitmap bitmap;
答案 0 :(得分:1)
我今天遇到了同样的问题。写完getInputStream();
之后尝试拨打OutputStream
:
...
con.setRequestMethod("POST");
con.setDoOutput(true);
con.setDoInput(true);
OutputStream os = con.getOutputStream();
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
bw.write(GetPOSTURL(ImageHashmap));
bw.flush();
bw.close();
InputStream is = con.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while ((tempMessage = br.readLine()) != null){
a = tempMessage;
}
...
我完全不知道为什么会这样。 getInputStream()
似乎以某种方式关闭OutputStream
。也许这与新的Android Studio 2.2有关,因为我之前从未注意到这种行为。