在 android 6 及更高版本中不显示图片

doesn#39;t show picture in android 6 and above(在 android 6 及更高版本中不显示图片)

本文介绍了在 android 6 及更高版本中不显示图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何在应用程序中使用相机,这就是我所达到的,这个想法是有一个打开相机的按钮,并且在我们拍照后照片会立即显示在屏幕上,第二个按钮来自早期版本,无法立即显示图片,必须单击才能显示.

I am trying to learn how to use the camera in an app and this is what I reached , the idea is having a button that opens the camera and that the picture will instantly show on the screen after we take the picture, the second button is from an earlier version that couldn't show the picture immediately and had to be clicked in order to show it.

无论如何我的问题是这段代码没有在 android 6 上显示图片.在我的 android 5 设备上它工作正常.图片保存在sdcard/camera_app/cam_image.jpg"路径中该按钮无法正常工作,所以我在想有关 imageview 的某些内容已从 android 5 更改为 6?问题几乎是如何使这项工作适用于 android 6 手机

Anyways my problem is that this code doesn't show the picture at android 6.. on my android 5 device it works fine.. the picture is saved in the "sdcard/camera_app/cam_image.jpg" path either way and the button doesn't work as well so I'm thinking something about the imageview has changed from android 5 to 6? the question is pretty much how to make this work for android 6 phones

public class Add_Comment_Picture extends AppCompatActivity {
    static final int CAM_REQUEST = 1;
    ImageView imageView;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add__comment__picture);
        button = (Button) findViewById(R.id.button);
        imageView = (ImageView) findViewById(R.id.imageView);
        Button button2 = (Button) findViewById(R.id.button3);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                File file = getFile();
                camera_intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                startActivityForResult(camera_intent , CAM_REQUEST);
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String path = "sdcard/camera_app/cam_image.jpg";
                imageView.setImageDrawable(Drawable.createFromPath(path));

            }
        });




    }

    private File getFile()
    {
        File folder = new File("sdcard/camera_app");
        if (!folder.exists())
        {
            folder.mkdir();
        }
        File image_file = new File(folder,"cam_image.jpg");
        return image_file;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        String path = "sdcard/camera_app/cam_image.jpg";
        imageView.setImageDrawable(Drawable.createFromPath(path));
        super.onActivityResult(requestCode, resultCode, data);
    }

}

推荐答案

您需要以编程方式授予 READ_EXTERNAL_STORAGE 和 WRITE_EXTERNAL STORANGE 权限.

You need to give READ_EXTERNAL_STORAGE and WRITE_EXTERNAL STORANGE permissions programmatically.

清单权限在 Android 6 上不起作用

使用棉花糖(最新版本的 Android).我们在使用 敏感 权限方面有一些限制,例如:存储、联系人访问等.在版本中,要在清单中授予这些权限,我们需要在 运行时.

With marshmallow(newest version of Android). We have got some restrictions in Using Sensitive permissions like : Storage,Contacts access, etc..In edition to give these permissions in manifest, We need to request them from users at Runtime.

有关更多详细信息,请参阅:Android M 权限

For more details refer this : Android M permissions

有关编码参考,请参阅此 SO 问题:Android marshmallow 请求权限?

For coding reference please refer this SO question : Android marshmallow request permission?

在您的活动中添加此代码:

Add this code in your activity :

@Override
protected void onStart() {
    super.onStart();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

        int hasWritePermission = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
        int hasReadPermission = checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
        int hasCameraPermission = checkSelfPermission(Manifest.permission.CAMERA);

        List<String> permissions = new ArrayList<String>();
        if (hasWritePermission != PackageManager.PERMISSION_GRANTED) {
            permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);

        } 

        if (hasReadPermission != PackageManager.PERMISSION_GRANTED) {
            permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);

        } 

        if (hasCameraPermission != PackageManager.PERMISSION_GRANTED) {
            permissions.add(Manifest.permission.CAMERA);

        } 
        if (!permissions.isEmpty()) {
            requestPermissions(permissions.toArray(new String[permissions.size()]), 111);
        }
    }


}

在 onActivityResult 之后添加:

Add this after onActivityResult :

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    switch (requestCode) {
        case 111: {
            for (int i = 0; i < permissions.length; i++) {
                if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                    System.out.println("Permissions --> " + "Permission Granted: " + permissions[i]);


                } else if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
                    System.out.println("Permissions --> " + "Permission Denied: " + permissions[i]);

                }
            }
        }
        break;
        default: {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }
}

这篇关于在 android 6 及更高版本中不显示图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:在 android 6 及更高版本中不显示图片

基础教程推荐