Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/391.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android Base64图像上传进度_Java_Android_Android Asynctask_Progressdialog - Fatal编程技术网

Java Android Base64图像上传进度

Java Android Base64图像上传进度,java,android,android-asynctask,progressdialog,Java,Android,Android Asynctask,Progressdialog,我有asyncstask,它可以帮助我将图像上传到服务器上,它可以完美地工作,没有任何问题,但现在我想显示上传百分比以及上传到用户的总大小。对于图像上传,我使用的是Base64图像,它在上传前被转换成字符串,显示百分比对我来说是一个问题,我在谷歌上搜索过,但无法找到Base64图像上传百分比的解决方案 这是我的任务 private class uploadImage extends AsyncTask<Void,Void,String> { private static fi

我有
asyncstask
,它可以帮助我将图像上传到服务器上,它可以完美地工作,没有任何问题,但现在我想显示上传百分比以及上传到用户的总大小。对于图像上传,我使用的是
Base64
图像,它在上传前被转换成字符串,显示百分比对我来说是一个问题,我在谷歌上搜索过,但无法找到Base64图像上传百分比的解决方案

这是我的任务

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");     */

    }
私有类上载映像扩展异步任务{
私有静态最终字符串上载图像\u URL=”https://192.168.1.10/Android/upload.php";
缓冲读取器;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
}
@凌驾
受保护的void onProgressUpdate(void…值){
super.onProgressUpdate(值);
}
@凌驾
受保护的字符串背景(无效…无效){
位图base64Bitmap=BitmapFactory.decodeFile(finalPath);
字符串Base64Image=App_函数。将_转换为_Base64(base64Bitmap);
int length=Base64Image.length();
试一试{
字符串数据=urlcoder.encode(“userId”,“UTF-8”)+“=”+urlcoder.encode(Session.getUserID(),“UTF-8”);
数据+=“&”+URLEncoder.encode(“ImageBase64”,“UTF-8”)+“=”+URLEncoder.encode(Base64Image,“UTF-8”);
//数据+=“&”+URLEncoder.encode(“类型”,“UTF-8”)+“=”+URLEncoder.encode(“封面照片”,“UTF-8”);
URL URL=新的URL(上传图像\u URL);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setRequestMethod(“POST”);
connection.setDoInput(true);
connection.setDoOutput(真);
connection.setConnectTimeout(10000);
connection.connect();
int imagePayload=Base64.decode(Base64Image,Base64.NO_WRAP).length;
Log.d(标签“图像有效载荷”+imagePayload/1024);
OutputStream OutputStream=connection.getOutputStream();
BufferedWriter writer=新的BufferedWriter(新的OutputStreamWriter(outputStream,UTF-8));
writer.write(数据);
writer.flush();
writer.close();
outputStream.close();
Log.d(标记“响应代码”+connection.getResponseCode());
if(connection.getResponseCode()==200){
InputStream InputStream=connection.getInputStream();
reader=新的BufferedReader(新的InputStreamReader(inputStream));
弦线;
StringBuilder stringBuffer=新的StringBuilder();
而((line=reader.readLine())!=null){
stringBuffer.append(行);
d(标记,“将JSON追加到字符串缓冲区”);
}
Log.d(标记“JSON接收”+stringBuffer);
JSONObject JSONObject=newJSONObject(stringBuffer.toString());
String serverResponse=jsonObject.getString(“userImageUpload”);
字符串pathOfImage,pathOfCover;
if(serverResponse.equals(“上传图像”)){
返回“成功”;
}否则{
返回“失败”;
}
}
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(格式错误){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}捕获(JSONException e1){
e1.printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串结果){
super.onPostExecute(结果);
如果(结果!=null){
如果(结果等于(“成功”)){
//progressBar.disclose();
}否则{
Toast.makeText(MyApplication.GetMcContext(),“无法上载图像。请重试。”,Toast.LENGTH_SHORT).show();
//progressBar.setProgress(0);
//progressBar.setMessage(“图像上传失败”);
//progressBar.setCancelable(真);
}
}
/*DialogFragment利息=新利息网格();
show(getFragmentManager(),“UploadImage”)*/
}

我认为BufferedWriter的这个方法可能有帮助:write(字符串s、int off、int len)但是您需要为每次调用指定部件的长度,计算自己在循环中写入的字符…您可以使用
改型
实现这一点。我不想使用任何库来显示百分比。我当前的代码不可能吗?