gpt4 book ai didi

android - 无法使用相机android获取图像

转载 作者:行者123 更新时间:2023-11-29 19:12:46 26 4
gpt4 key购买 nike

应用程序应该从相机或画廊(用户选择)获取图像,然后将其上传到服务器。从图库中获取图像并将其上传到服务器没有问题。这里的问题是检索相机拍摄的图像!

public class UploadActivity extends AppCompatActivity implements View.OnClickListener {

private Button UploadBn;
private ImageButton ChooseBn, CameraBn;
private EditText NAME;
private ImageView imgView;
private CameraPhoto cameraPhoto;
private GalleryPhoto galleryPhoto;
final int CAMERA_REQUEST=13323;
final int GALLERY_REQUEST=22131;
private String selectedPhoto;
private Bitmap bitmap = null;
private String UploadUrl="http://localhost/webapp/getImg.php";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload);

NAME=(EditText)findViewById(R.id.name);
UploadBn=(Button)findViewById(R.id.uploadBn);
ChooseBn=(ImageButton) findViewById(R.id.ivGallery);
CameraBn=(ImageButton) findViewById(R.id.ivCamera);
imgView=(ImageView)findViewById(R.id.imageView);
cameraPhoto = new CameraPhoto(getApplicationContext());
galleryPhoto = new GalleryPhoto(getApplicationContext());
ChooseBn.setOnClickListener(this);
UploadBn.setOnClickListener(this);
CameraBn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.ivGallery:
selectImageGalerie();
break;

case R.id.uploadBn:
if(NAME==null || imgView.getDrawable()==null)
Toast.makeText(this,"select an image, give it a name",Toast.LENGTH_LONG).show();
else
uploadImage();
break;
case R.id.ivCamera:
selectImageCamera();
break;
}
}

private void selectImageGalerie() {

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

private void selectImageCamera() {
try {
startActivityForResult(cameraPhoto.takePhotoIntent(), CAMERA_REQUEST);
cameraPhoto.addToGallery();
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Somathing Wrong while taking photos", Toast.LENGTH_SHORT).show();
}
}

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() {
final ProgressDialog loading = ProgressDialog.show(this,"Uploading...","Please wait...",false,false);
StringRequest stringRequest=new StringRequest(Request.Method.POST, UploadUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
Toast.makeText(UploadActivity.this, response , Toast.LENGTH_LONG).show();
imgView.setImageResource(0);
imgView.setVisibility(View.GONE);
NAME.setText("");
NAME.setVisibility(View.GONE);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
loading.dismiss();
Toast.makeText(UploadActivity.this, error.getMessage().toString(), Toast.LENGTH_LONG).show();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params=new HashMap<>();
params.put("name",NAME.getText().toString().trim()+".jpg");
params.put("encoded",imageToString(bitmap));
return params;
}
};
MySingleton.getInstance(UploadActivity.this).addToRequestQue(stringRequest);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode==RESULT_OK && data != null){
if(requestCode==GALLERY_REQUEST) {
Uri path = data.getData();
galleryPhoto.setPhotoUri(path);
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), path);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else if(requestCode==CAMERA_REQUEST) {
String photoPath=cameraPhoto.getPhotoPath();
selectedPhoto=photoPath;
try {
Log.d("BITMAP ==", "AWEL EL TRY");
bitmap= ImageLoader.init().from(photoPath).requestSize(300,300).getBitmap();
imgView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
Toast.makeText(getApplicationContext(),"Something Wrong while choosing photos",Toast.LENGTH_SHORT).show();
}
}
imgView.setImageBitmap(bitmap);
imgView.setVisibility(View.VISIBLE);
NAME.setVisibility(View.VISIBLE);
Log.d("BITMAP ==", "E5ER EL TRY");
}
}

在执行一些测试后,我发现变量 datanull 所以它会跳过 if() 中的所有语句 < em>onActivityResult() 方法。没道理,为什么我拍了照片,相机没有返回值!

最佳答案

why would the camera not return a value even though I took a picture

如果您在Intent 上提供EXTRA_OUTPUT,则ACTION_IMAGE_CAPTURE 不需要返回结果。我假设这就是 takePhotoIntent() 所做的。

此外,requestCode==CAMERA_REQUEST 分支的其余代码无论如何都不使用data Uri

关于android - 无法使用相机android获取图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44677729/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com