ImageView无法在智能手机Android 6.0.1上显示来自图库的图像

时间:2017-10-25 07:13:33

标签: android imageview

我想要从Gallery到ImageView的显示图像。在模拟器Android 4.2.2中,我的代码可以按照我的预期运行。但是当我插入智能手机Android 6.0.1时,无法将图像显示到ImageView 这是我的代码:

//Open gallery code
private void showFileChooser() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
    }


//Set ImageView code
@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

            filePath = data.getData();
            try {
                imageView = (ImageView) getActivity().findViewById(R.id.imageView);
                bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);
                imageView.setImageBitmap(bitmap);

            } catch (IOException e) {
                e.printStackTrace();
            }
    }

我的问题。这是因为我的Android版本?以及如何解决这个问题? 感谢

3 个答案:

答案 0 :(得分:0)

link会对您有所帮助。只需在项目中添加 ImageFilePath.java 即可解决您的问题。

答案 1 :(得分:0)

从Android 6.0(API级别23)开始,用户在应用运行时向应用授予权限,而不是在他们安装应用时



if (ContextCompat.checkSelfPermission(getActivity(),
                        Manifest.permission.CAMERA)
                        != PackageManager.PERMISSION_GRANTED) {
                    // User may have declined earlier, ask Android if we should show him a reason

                    if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.CAMERA)) {
                        // show an explanation to the user
                        ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_CAPTURE_IMAGE);

                        // Good practise: don't block thread after the user sees the explanation, try again to request the permission.
                    } else {
                        // request the permission.
                        // CALLBACK_NUMBER is a integer constants
                        ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, PERMISSIONS_REQUEST_CAPTURE_IMAGE);
                        // The callback method gets the result of the request.
                    }
                } else {

                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent, "Select Picture"), 12);

                }




在onActivityResult(int requestCode,int resultCode,Intent data)



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

        if(requestCode==12)
        {
            if(resultCode==RESULT_OK)
            {

                if (data == null) {
                    //Display an error
                    Utils.Toast_S(getActivity(), "No Image Data");

                    return;
                }
                try {
                     inputStream = getContext().getContentResolver().openInputStream(data.getData());

                    imageView.setImageBitmap(BitmapFactory.decodeStream(inputStream));

                    File  file=new File("");
                    file.getName();
                    file.getAbsolutePath();



                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    Toast.makeText(getContext(), "Exception", Toast.LENGTH_SHORT).show();
                }


            }
        }

    }




答案 2 :(得分:0)

系统权限分为几个保护级别。要了解的两个最重要的保护级别是正常和危险的权限:

  

正常权限涵盖了应用需要访问应用程序沙箱外部数据或资源的区域,但用户隐私或其他应用程序操作的风险非常小。例如,设置时区的权限是正常权限。如果应用声明它需要正常权限,系统会自动向该应用授予权限。有关当前正常权限的完整列表,请参阅正常权限。

     

危险权限涵盖应用程序需要涉及用户私人信息的数据或资源的区域,或者可能潜在地影响用户的存储数据或其他应用程序的操作。例如,阅读用户联系人的能力是一种危险的许可。如果应用声明它需要危险权限,则用户必须明确授予该应用的权限:

[链接] https://developer.android.com/guide/topics/permissions/requesting.html