没有在Android 6及以上

时间:2016-05-17 06:59:41

标签: android imageview android-camera onactivityresult

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

无论如何我的问题是这段代码没有在android 6上显示图片..在我的android 5设备上它工作正常..图片保存在" sdcard / camera_app / cam_image.jpg& #34;路径任何一种方式和按钮都不起作用所以我在思考一些关于imageview的东西已经从android 5改为6?问题是如何让这个工作为Android 6手机

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);
    }

}

2 个答案:

答案 0 :(得分:6)

您需要以编程方式提供READ_EXTERNAL_STORAGE和WRITE_EXTERNAL STORANGE权限。

在Android 6上无法使用明确的许可

使用marshmallow(Android的最新版本)。我们在使用敏感权限方面遇到了一些限制:存储,联系人访问等。在版本中,要在清单中提供这些权限,我们需要在运行时向用户请求这些权限>

有关详细信息,请参阅:Android M permissions

有关编码参考,请参阅此问题:Android marshmallow request permission?

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

@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之后添加:

@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);
        }
    }
}

答案 1 :(得分:2)

android 6.0权限结构发生变化后。您必须检查运行时的权限。例如,您将从图库中选择一张图片,用户在进入图库之前授予图库访问权限。

您可以查看此文档以获取此新功能。

https://developer.android.com/training/permissions/requesting.html

您的问题的示例代码

if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED) {
        Log.v(TAG,"Permission is granted");
        return true;
  }

如果没有,您需要让用户授予您的应用权限:

ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
祝你好运:)