图像未显示在ImageView
中。我可以选择图像或拍摄新图像的照片,但是该图像不会显示在ImageView
中。
public class RequestQuoteFragment extends Fragment {
ImageButton mPostImage;
ImageView imageView;
private EditText mDescription, mWidth, mLength, mHeight;
Integer REQUEST_CAMERA = 1, SELECT_FILE = 2;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_request_quotes, container, false);
imageView = view.findViewById(R.id.image_view);
mDescription = view.findViewById(R.id.item_description);
mWidth = view.findViewById(R.id.width);
mLength = view.findViewById(R.id.length);
mHeight = view.findViewById(R.id.height);
mPostImage = view.findViewById(R.id.attach_file);
mPostImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
attachFile();
}
});
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
return view;
}
//Attach Activity to fragments
private Context mContext;
@Override
public void onAttach(final Activity activity) {
super.onAttach(activity);
mContext = activity;
}
private void attachFile() {
final CharSequence[] items = {"Camera", "Gallery", "Cancel"};
AlertDialog.Builder builder;
builder = new AlertDialog.Builder(mContext);
builder.setTitle("Add Image");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if(items[i].equals("Camera")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
} else if (items[i].equals("Gallery")) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent.createChooser(intent,"Select Photo"), SELECT_FILE);
} else if (items[i].equals("Cancel")) {
dialogInterface.dismiss();
}
}
});
builder.show();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
Bundle bundle = data.getExtras();
final Bitmap bitmap = (Bitmap) bundle.get("data");
imageView.setImageBitmap(bitmap);
} else if (requestCode == SELECT_FILE) {
Uri selectImageUri = data.getData();
imageView.setImageURI(selectImageUri);
}
}
}
//reset fields
private void resetFields() {
//UniversalImageLoader.setImage("", mPostImage);
mDescription.setText("");
mWidth.setText("");
mLength.setText("");
mHeight.setText("");
}
}
答案 0 :(得分:0)
这个对我有用。
Intent getIntent = new Intent(Intent.ACTION_GET_CONTENT).setType("image/*");
Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
Intent chooserIntent = Intent.createChooser(getIntent, "Select Profile Image");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {pickIntent});
startActivityForResult(chooserIntent, AppConstants.PICK_IMAGE);
和
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK && requestCode == AppConstants.PICK_IMAGE)
{
image.setImageURI(data.getData());
}
}
已编辑
否则
startActivityForResult(intent.createChooser(intent,“ Select Photo”),SELECT_FILE);
将以上行更改为
startActivityForResult(Intent.createChooser(intent,"Select Photo"), SELECT_FILE);
答案 1 :(得分:0)
您的代码上有一个小错误,您必须检查 resultCode == Activity.RESULT_OK ,而不是 requestCode == Activity.RESULT_OK
希望这会帮助您解决问题。谢谢!
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == REQUEST_CAMERA) {
Bundle bundle = data.getExtras();
final Bitmap bitmap = (Bitmap) bundle.get("data");
imageView.setImageBitmap(bitmap);
} else if (requestCode == SELECT_FILE) {
Uri selectImageUri = data.getData();
imageView.setImageURI(selectImageUri);
}
}
}
答案 2 :(得分:0)