如何使 "ImageView" 字段在 android 中成为必填项?
How to make a "ImageView" field as mandatory in android?
我的 android 应用程序中有一个 imageview 字段,单击它我提供了 "open camera" 功能,用户可以单击相机中的照片并将其上传到 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);
}
}
}
);
我想要的是,如果我没有在图像视图中上传图像,那么它应该将 error/toast 消息显示为 "Image not uploaded"
您好,您可以尝试这样的操作
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);
}
- 就是这么简单。当您调用 Intent for camera open 时,它会在 onActivityResult 中提供回调,因为它只传递图像的值。并在点击事件中检查是否为null。
Example :-
=> 打开相机 :-
Intent intent1 = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent1, camera);
- 您将在 onActivityResult 中获得回调
=> 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;
}
- 终于imagepath有了图片的实际路径。
=> 检查图像路径是否为 null :-(在事件中)
if (imagepath == null) {
Toast.makeText(this, "Please Select FileImage", Toast.LENGTH_SHORT).show();
}
如果附加到 ImageView,您可以检查 Drawable
Imageview imageView = (ImageView)findViewById(R.id.image);
if(imageView.getDrawable() == null){
//Then Nothing is attached with imageviw
}
我的 android 应用程序中有一个 imageview 字段,单击它我提供了 "open camera" 功能,用户可以单击相机中的照片并将其上传到 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);
}
}
}
);
我想要的是,如果我没有在图像视图中上传图像,那么它应该将 error/toast 消息显示为 "Image not uploaded"
您好,您可以尝试这样的操作
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);
}
- 就是这么简单。当您调用 Intent for camera open 时,它会在 onActivityResult 中提供回调,因为它只传递图像的值。并在点击事件中检查是否为null。
Example :-
=> 打开相机 :-
Intent intent1 = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent1, camera);
- 您将在 onActivityResult 中获得回调
=> 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;
}
- 终于imagepath有了图片的实际路径。
=> 检查图像路径是否为 null :-(在事件中)
if (imagepath == null) {
Toast.makeText(this, "Please Select FileImage", Toast.LENGTH_SHORT).show();
}
如果附加到 ImageView,您可以检查 Drawable
Imageview imageView = (ImageView)findViewById(R.id.image);
if(imageView.getDrawable() == null){
//Then Nothing is attached with imageviw
}