【问题标题】:Upload from camera intent从相机意图上传
【发布时间】:2018-07-18 12:50:52
【问题描述】:

目前正在开发小型应用程序来自学。我需要捕获图像,在 imageview 中显示并上传。我已经设法创建该意图并在 imageview 上显示图像,但目前无法上传它并且无法弄清楚如何?还有从图库上传图像的按钮。我应该使用异步任务吗?我迷路了

相机意图:

mCapture.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, REQUEST_CAMERA_CAPTURE);
        }
    });

图片上传:

private void UploadImage (){

    StringRequest stringRequest = new StringRequest(Request.Method.POST, UploadUrl,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        String Response = jsonObject.getString("response");
                        Toast.makeText(MainActivity.this,Response,Toast.LENGTH_SHORT).show();
                        mImageView.setImageResource(0);
                        mImageView.setVisibility(View.GONE);
                        mEditText.setText("");
                        mEditText.setVisibility(View.GONE);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {

        }
    })


    {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            Map<String,String> params = new HashMap<>();
            params.put("name1",mEditText.getText().toString().trim());
            params.put("name2", staticSpinner.getSelectedItem().toString().trim());
            params.put("image",ImageToString(bitmap));

            return params;
        }
    };

图像转字符串

    private String ImageToString(Bitmap bitmap){

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 25, byteArrayOutputStream);
    byte [] imgBytes = byteArrayOutputStream.toByteArray();

    return Base64.encodeToString(imgBytes, Base64.DEFAULT);
}

【问题讨论】:

    标签: android imageview android-camera


    【解决方案1】:

    你应该上传你的图像的字节数组下面是位图转换为字节数组的代码。

    Bitmap bmp = intent.getExtras().get("data"); // your image bitmap
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();  send this b
    bmp.recycle(); // recycle your bitmap
    

    【讨论】:

    • 你必须在多部分实体中上传图片
    • 只是想知道我是否必须在数据库中创建该 url?没看出我很重要,谢谢你的帮助,我会尽快尝试的
    • 那个链接没什么用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多