在android 6及更高版本中不显示图片 [英] doesn't show picture in android 6 and above

查看:162
本文介绍了在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.

无论如何我的问题是这个代码没有t在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.

MANIFEST PERMISSIONS将无法在Android 6上运行

使用marshmallow(最新版本) 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

有关编码参考,请参阅以下问题: 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及更高版本中不显示图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆