Android Base64图像上传进度

时间:2019-10-23 02:00:55

标签: java android android-asynctask progressdialog

我有AsynscTask可以帮助我将图像上传到服务器,并且工作正常,没有任何问题,但是现在我想显示上传百分比并将其总大小上传给用户。对于图像上传,我正在使用Base64图像,该图像在上传之前会转换为String并显示百分比,这对我来说是个问题,但我找不到Base64图像上传百分比的解决方案。

这是我的AsyncTask

private class uploadImage extends AsyncTask<Void,Void,String> {
    private static final String uploadImage_URL="https://192.168.1.10/Android/upload.php";
    BufferedReader reader;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected String doInBackground(Void... voids) {
        Bitmap base64Bitmap = BitmapFactory.decodeFile(finalPath);
        String Base64Image = App_Functions.Convert_To_Base64(base64Bitmap);
        int length = Base64Image.length();

        try {
            String data = URLEncoder.encode("userId", "UTF-8") + "=" + URLEncoder.encode(Session.getUserID(), "UTF-8");
            data += "&" + URLEncoder.encode("ImageBase64", "UTF-8") + "=" + URLEncoder.encode(Base64Image, "UTF-8");
            //data += "&" + URLEncoder.encode("type", "UTF-8") + "=" + URLEncoder.encode("coverphoto", "UTF-8");
            URL url=new URL(uploadImage_URL);
            HttpURLConnection connection= (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setConnectTimeout(10000);
            connection.connect();
            int imagePayload = Base64.decode(Base64Image,Base64.NO_WRAP).length;
            Log.d(TAG,"Image Pay Load "+imagePayload/1024);

            OutputStream outputStream = connection.getOutputStream();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
            writer.write(data);
            writer.flush();
            writer.close();
            outputStream.close();

            Log.d(TAG, "Response Code " + connection.getResponseCode());
            if (connection.getResponseCode() == 200) {
                InputStream inputStream = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(inputStream));

                String line;
                StringBuilder stringBuffer = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    stringBuffer.append(line);
                    Log.d(TAG, "Appending JSON into String Buffer");
                }
                Log.d(TAG, "JSON Received " + stringBuffer);
                JSONObject jsonObject = new JSONObject(stringBuffer.toString());

                String serverResponse = jsonObject.getString("userImageUpload");
                String pathOfImage,pathOfCover;
                if (serverResponse.equals("Image Uploaded")){
                    return "Success";
                }else {
                    return "Fail";
                }

            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e1) {
            e1.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);      
        if (result!=null){
            if (result.equals("Success")){
                //progressBar.dismiss();

            }else {
                Toast.makeText(MyApplication.getmContext(), "Fail to upload image. Please try again.", Toast.LENGTH_SHORT).show();
                //progressBar.setProgress(0);
                //progressBar.setMessage("Image Upload Failed");
                //progressBar.setCancelable(true);
            }
        }

        /*DialogFragment interest=new Interest_Grid();
        interest.show(getFragmentManager(),"UploadImage");     */

    }

0 个答案:

没有答案