我有一个需要位图的Web服务。 Choosing from gallery
选项效果很好,可以将图像上传到服务器,但是我无法在Take photo
上使用它
在这两种方法中,我都可以在上传(审阅)之前将位图设置为我的imageView。因此,它会获取位图并显示在imageview中,但不会上传。 你有什么想法吗?提前致谢! 这是我的课;
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();
}
});
}
答案 0 :(得分:0)
我已通过压缩图像来解决问题,因为它的大小超过5MB,我认为字节有问题。