简体   繁体   English

Android ImageView无法从图库或相机中获取图像

[英]Android ImageView can't get image from gallery or camera

I want to pick a photo from gallery or camera on Android and show the photo on ImageView, open gallery or camera was fine and there's no problem with permission. 我想从Android上的图片库或相机中选择一张照片,然后在ImageView上显示该照片,打开图片库或相机就可以了,没有问题。 But the problem is, the picture that I select or I take from the camera was not shown on ImageView, and there's no error. 但是问题是,我选择或从相机拍摄的照片没有显示在ImageView上,并且没有错误。

Below is my code 下面是我的代码

private static final int CAMERA_REQUEST_CODE = 1;
private static final int SELECT_FILE = 0;

public PersonalDataStep(){
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.personal_data_step, container, false);
    ButterKnife.bind(this, view);

    //imageEktp.setClickable(true);

    getPhoto.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Log.d("test : ", "testing");
            CharSequence menu[] = new CharSequence[]{"Take From Galery", "Open Camera"};
            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
            builder.setTitle("Pick a Picture");
            builder.setItems(menu, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    if(i == 0){
                        Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        intent.setType("image/*");
                        startActivityForResult(intent, SELECT_FILE);
                    }else if(i == 1){
                        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        startActivityForResult(intent, CAMERA_REQUEST_CODE);

                    }
                }
            });
            builder.show();
        }
    });

    return view;
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode== Activity.RESULT_OK) {
        if (resultCode == CAMERA_REQUEST_CODE) {
            Bundle bundle = data.getExtras();
            final Bitmap bitmap = (Bitmap) bundle.get("data");
            imageEktp.setImageBitmap(bitmap);
        } else if (resultCode == SELECT_FILE) {
            Uri selectedImage = data.getData();
            imageEktp.setImageURI(selectedImage);
            Log.d("image from galery : ", selectedImage.toString());
        }
    }else{
        Toast.makeText(getActivity(), "You haven't picked Image",Toast.LENGTH_LONG).show();
    }
}

Try it , a little change to your method, it works well for me. 尝试一下 ,对您的方法进行一些更改,对我来说效果很好。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode== Activity.RESULT_OK) {
        if (requestCode == CAMERA_REQUEST_CODE && data! = null) {
            Uri mImageCaptureUri = data.getData();
             imageEktp.setImageURI(mImageCaptureUri);
        } else if (requestCode == SELECT_FILE && data != null) {
            Uri selectedImage = data.getData();
            imageEktp.setImageURI(selectedImage);
            Log.d("image from galery : ", selectedImage.toString());
        }
    }else{
        Toast.makeText(getActivity(), "You haven't picked Image",Toast.LENGTH_LONG).show();
    }
}

try this code: 试试这个代码:

@Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
 super.onActivityResult(requestCode, resultCode, data);
 if (resultCode == Activity.RESULT_OK) {
 if (requestCode == SELECT_FILE)
    onSelectFromGalleryResult(data);
 else if (requestCode == REQUEST_CAMERA)
    onCaptureImageResult(data);
 }
 }
@SuppressWarnings("deprecation")
private void onSelectFromGalleryResult(Intent data) {
 Bitmap bm=null;
 if (data != null) {
 try {
    bm=MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), 
data.getData());
 } catch (IOException e) {
    e.printStackTrace();
 }
 }
 ivImage.setImageBitmap(bm);
  }
 private void onCaptureImageResult(Intent data) {
 Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
 ByteArrayOutputStream bytes = new ByteArrayOutputStream();
 thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
 File destination = new File(Environment.getExternalStorageDirectory(),
    System.currentTimeMillis() + ".jpg");
 FileOutputStream fo;
 try {
 destination.createNewFile();
 fo = new FileOutputStream(destination);
 fo.write(bytes.toByteArray());
 fo.close();
 } catch (FileNotFoundException e) {
 e.printStackTrace();
 } catch (IOException e) {
 e.printStackTrace();
 }
 ivImage.setImageBitmap(thumbnail);
 }

i think it helps you please check your code once. 我认为这可以帮助您检查一下您的代码。

Small typo in your code 代码中的小错字

if (resultCode == CAMERA_REQUEST_CODE) {
 ...
} else if (resultCode == SELECT_FILE) {
 ...
}

Should be 应该

if (requestCode == CAMERA_REQUEST_CODE) {
 ...
} else if (requestCode == SELECT_FILE) {
 ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将图像从Android中的图片库设置为imageview - Can't set the image to imageview from image gallery in Android 从相机获取图像或从图库上传并在 ImageView (Android Studio) 中显示后,保存图像为 SharedPreferences - Save image is SharedPreferences after Image is get from camera or upload from gallery and display at ImageView (Android Studio) 如何在图库中的imageView上设置图像以及在Android中由相机拍摄的图像? - How to set an image on imageView from the gallery and image taken by camera in Android? Android 从图库中获取图像到 ImageView - Android get image from gallery into ImageView Android,从图库中获取图像到 imageView - Android, get image from gallery into a imageView 无法在DialogFragmentAndroid中设置来自画廊或相机的图像 - Can't set image from gallery or camera in dialogFragment android 安卓 | 无法从图库中获取图像 - Android | Can't get image from gallery 为什么我无法从Android图库中的相机相册中获取图像? - Why can't I get an image from the camera album in Android gallery? 从图库或相机设置图像以适合Imageview android - set image from Gallery or Camera to fit to Imageview android Android - 从图库/相机获取图像时,Dialog不会刷新ImageView - Android - Dialog not refresing ImageView when getting image from gallery/ camera
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM