我的android应用中有一个imageview字段,在onclick上我已赋予它“打开相机”功能,用户可以单击相机中的照片并将其上传到ImageView
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(user_image.getDrawable() == null ){
Toast.makeText(NewCall4.this, "No image uploaded", Toast.LENGTH_SHORT).show();
}
else{
Intent i=new Intent(NewCall4.this,NewCall5.class);
startActivity(i);
}
}
}
);
我想要的是,如果我没有在imageview中上传图片,那么它应该将错误/敬酒消息显示为“未上传图片”
答案 0 :(得分:1)
示例:-
=> 要打开相机:-
Intent intent1 = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent1, camera);
=> onActivityResult:-(我在这里获得了实际的图像路径)
if (resultCode == RESULT_OK && requestCode == camera) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
addprofile.setImageBitmap(photo); // set image to imageview
Uri tempUri = getImageUri(getApplicationContext(), photo);
File finalfile = new File(getrealpathfromuri(tempUri));
imagepath = finalfile.toString(); // imagepath is a global variable
}
private Uri getImageUri(Context applicationContext, Bitmap photo) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(applicationContext.getContentResolver(),photo,"Title",null);
return Uri.parse(path);
}
private String getrealpathfromuri(Uri tempUri) {
String path = "";
if(getContentResolver() != null){
Cursor cursor = getContentResolver().query(tempUri, null, null, null, null);
if(cursor != null){
cursor.moveToFirst();
int idk = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
path = cursor.getString(idk);
cursor.close();
}
}
return path;
}
=> 检查图像路径是否为空 :-(如果有)
if (imagepath == null) {
Toast.makeText(this, "Please Select FileImage", Toast.LENGTH_SHORT).show();
}
答案 1 :(得分:0)
嗨,您可以尝试这样的事情
Imageview img = (ImageView)findViewById(R.id.image){
img .setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, id);
}
});
和OnResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap photo = (Bitmap) data.getExtras().get(“data”);
img.setImageBitmap(photo);
}
答案 2 :(得分:0)
您可以检查Drawable是否附加到ImageView
Imageview imageView = (ImageView)findViewById(R.id.image);
if(imageView.getDrawable() == null){
//Then Nothing is attached with imageviw
}