Android 图库图像转为 Base 64 字符串

发布于 2024-12-18 18:07:09 字数 2584 浏览 0 评论 0原文

我的应用程序允许用户从图库中选择图像并将其上传到服务器。目前我可以显示图像但不能上传它。我没有收到错误。下面是我的代码,我希望它是清楚的。

public void submit_click(View view) {
    TextView err=(TextView) findViewById(R.id.err);
    if (thumbnail != null) {
        // TextView err = (TextView) findViewById(R.id.err);
        try{
            byte[] bitmapdata = imagetoArray();
            img = decodeUTF8(bitmapdata);

            if (updateImage() == true) {
                Intent myIntent = new Intent(view.getContext(), ProfileActivity.class);
                startActivityForResult(myIntent, 0);    
            } else {
                err.setText("Error update image");
            }
        }
        catch(Exception ex)
        {
            err.setText(ex.getLocalizedMessage());
        }    
    }    
}

private boolean updateImage() {
    boolean status = false;
    TextView err=(TextView) findViewById(R.id.err);
    String username = SessionManager.getMaps("user");
    String postData = "{\"UserImage\":\"" + img + "\",\"UserName\":\"" + username + "\"}";
    try {
        String domain = getString(R.string.domain);
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        HttpPost httppost = new HttpPost(domain + "updateUserImage");
        StringEntity se = new StringEntity(postData.toString(), "utf-8");
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httppost.setEntity(se);
        httppost.setHeader("Accept", "application/json");
        httppost.setHeader("Content-type", "application/json");
        response = httpclient.execute(httppost);
        if (response != null) {
            HttpEntity r_entity = response.getEntity();
            String json = EntityUtils.toString(r_entity);
            status = Boolean.parseBoolean(json);
        }    
    } catch (Exception e) {
        err.setText(e.getLocalizedMessage());
    }

    return status;
}

private byte[] imagetoArray() {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    TextView err=(TextView) findViewById(R.id.err);
    try{           
        thumbnail.compress(Bitmap.CompressFormat.PNG, 100, stream);
    }
    catch(Exception ex)
    {
        err.setText(ex.getLocalizedMessage());
    }
    return stream.toByteArray();
}


private String decodeUTF8(byte[] bytes) {
    String sw="";
    TextView err=(TextView) findViewById(R.id.err);
    try{
        sw= Base64.encodeToString(bytes, Base64.NO_WRAP);
    }
    catch(Exception ex)
    {
        err.setText(ex.getLocalizedMessage());
    }
    return sw;
}

My app allows a user to select an image from the gallery and upload it to a server. Currently I can display the image but not upload it. I am not getting an error. Below is my code, I hope that it is clear.

public void submit_click(View view) {
    TextView err=(TextView) findViewById(R.id.err);
    if (thumbnail != null) {
        // TextView err = (TextView) findViewById(R.id.err);
        try{
            byte[] bitmapdata = imagetoArray();
            img = decodeUTF8(bitmapdata);

            if (updateImage() == true) {
                Intent myIntent = new Intent(view.getContext(), ProfileActivity.class);
                startActivityForResult(myIntent, 0);    
            } else {
                err.setText("Error update image");
            }
        }
        catch(Exception ex)
        {
            err.setText(ex.getLocalizedMessage());
        }    
    }    
}

private boolean updateImage() {
    boolean status = false;
    TextView err=(TextView) findViewById(R.id.err);
    String username = SessionManager.getMaps("user");
    String postData = "{\"UserImage\":\"" + img + "\",\"UserName\":\"" + username + "\"}";
    try {
        String domain = getString(R.string.domain);
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        HttpPost httppost = new HttpPost(domain + "updateUserImage");
        StringEntity se = new StringEntity(postData.toString(), "utf-8");
        se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
        httppost.setEntity(se);
        httppost.setHeader("Accept", "application/json");
        httppost.setHeader("Content-type", "application/json");
        response = httpclient.execute(httppost);
        if (response != null) {
            HttpEntity r_entity = response.getEntity();
            String json = EntityUtils.toString(r_entity);
            status = Boolean.parseBoolean(json);
        }    
    } catch (Exception e) {
        err.setText(e.getLocalizedMessage());
    }

    return status;
}

private byte[] imagetoArray() {
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    TextView err=(TextView) findViewById(R.id.err);
    try{           
        thumbnail.compress(Bitmap.CompressFormat.PNG, 100, stream);
    }
    catch(Exception ex)
    {
        err.setText(ex.getLocalizedMessage());
    }
    return stream.toByteArray();
}


private String decodeUTF8(byte[] bytes) {
    String sw="";
    TextView err=(TextView) findViewById(R.id.err);
    try{
        sw= Base64.encodeToString(bytes, Base64.NO_WRAP);
    }
    catch(Exception ex)
    {
        err.setText(ex.getLocalizedMessage());
    }
    return sw;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木落 2024-12-25 18:07:09

如果您想处理单击事件,最好向按钮添加事件处理程序。

      submit.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onClick(View v) {
            submit_click(v);
        }          
      });

PS:对你的程序员同事有一颗爱心,请阅读:Java 代码约定

If you want to handle a click event it would be a good idea to add a event handler to the button.

      submit.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onClick(View v) {
            submit_click(v);
        }          
      });

PS: Have a heart for your fellow programmer and read that: Java Code Conventions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文