简体   繁体   English

拍照并获取Bitmap&Uri-Android

[英]Take Photo and Get Bitmap&Uri - android

I have a web service which expects bitmap. 我有一个期望位图的Web服务。 Choosing from gallery option works well and uploads image to server, but I couldn't make it for Take photo Choosing from gallery选项中选择效果很好,并且可以将图像上传到服务器,但是我无法在Take photo

In both, I can set bitmap to my imageView before uploading (review). 在这两种方式中,我都可以在上传(审阅)之前将位图设置为我的imageView。 So, it gets bitmap and show in imageview but doesn't upload. 因此,它会获取位图并显示在imageview中,但不会上传。 Do you have an idea what's wrong? 你有什么想法吗? Thanks in advance! 提前致谢! Here is my class; 这是我的课;

public class TakePhoto extends AppCompatActivity implements View.OnClickListener{

private Button button, button2, button3;
private String encoded_string, image_name;
private Bitmap bitmap;
private File file;
private Uri file_uri;
private final int IMG_GALLERY = 1;
private final int IMG_CAMERA = 2;
private ImageView imageView;

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == IMG_GALLERY && resultCode == RESULT_OK && data != null){

        Uri uri_gallery = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri_gallery);
            imageView.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }else if(requestCode == IMG_CAMERA && resultCode == RESULT_OK && data != null){
        Uri uri_camera = data.getData();
        try {
            bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri_camera);
            imageView.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_take_photo);

    button = (Button) findViewById(R.id.button);
    button2 = (Button) findViewById(R.id.button2);
    button3 = (Button) findViewById(R.id.button3);
    imageView = findViewById(R.id.imageShow);
    button.setOnClickListener(this);
    button2.setOnClickListener(this);
    button3.setOnClickListener(this);
}

private void selectImage(){

    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(intent, IMG_GALLERY);

}

private void takeImage(){
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, IMG_CAMERA);
}

@Override
public void onClick(View v) {

    switch (v.getId()){

        case R.id.button:
            selectImage();
            break;

        case R.id.button2:
            uploadImage();
            break;

        case R.id.button3:
            takeImage();
            break;

    }


}

private String imageToString(Bitmap bitmap){
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
    byte[] imgBytes = byteArrayOutputStream.toByteArray();
    return Base64.encodeToString(imgBytes,Base64.DEFAULT);
}

private void uploadImage(){

    Map<String,String> map = new HashMap<String,String>();
    map.put("encoded_string",imageToString(bitmap));
    map.put("image_name","newimage");

    API_Service api_service = Client.getRetrofitInstance().create(API_Service.class);
    Call<ResponseBody> call = api_service.sendImage(map);
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            Toast.makeText(TakePhoto.this, ""+response.body().getSuccess(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            Toast.makeText(TakePhoto.this, "hata", Toast.LENGTH_SHORT).show();
        }
    });

}

我已经通过压缩图像来解决问题,因为它的大小超过5MB,并且我认为字节有问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM