1

我只是想编写一个代码来捕获图像并将该图像保存在名称为 test.jpg 的默认目录中。我的设备确实捕获了图像,但它运行测试用例的 else 部分并显示错误捕获图像。在 xml 文件中只有一个按钮并且 android:onClick 设置为处理

公共类 MainActivity 扩展 Activity {

private File imagefile;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public void process(View v)
{
    Toast.makeText(this,"Inside the process",Toast.LENGTH_SHORT).show();
    Intent  intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    imagefile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"test.jpg");
    Uri temp = Uri.fromFile(imagefile);
    intent.putExtra(MediaStore.EXTRA_OUTPUT,temp);
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1);
    startActivityForResult(intent,0);


}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode==0)
    {
        switch (resultCode){
            case Activity.RESULT_OK:
                if(imagefile.exists())
                {
                    Toast.makeText(this,"File was saved at "+imagefile.getAbsolutePath(),Toast.LENGTH_SHORT).show();
                }
                else
                {
                    Toast.makeText(this,"Error captureing image",Toast.LENGTH_SHORT).show();
                }
                break;
            case Activity.RESULT_CANCELED:
                break;
        }
    }
}
}
4

1 回答 1

0

1.使用checkSelfPermission

2.使用requestPermissions

3.使用onRequestPermissionsResult

  • 如果授予权限,您可以做您想做的事情,否则您应该requestPermissions在您的代码中做。

  • 如果您请求权限,您可以在方法中处理结果onRequestPermissionsResult。如果授予权限,您可以做您想做的事情,否则处理该权限被拒绝。

试试这个 。

public static final int MY_PERMISSIONS_REQUEST_CAMERA = 0;

// process in your code
public void process(View view) {
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this,
                new String[]{Manifest.permission.CAMERA},
                MY_PERMISSIONS_REQUEST_CAMERA);
    } else {
        // permission_granted
        callMethod();
    }
}

/**
 * do something you want
 */
public void callMethod() {
    Toast.makeText(this, "Inside the process", Toast.LENGTH_SHORT).show();
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    imagefile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "test.jpg");
    Uri temp = Uri.fromFile(imagefile);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, temp);
    intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
    startActivityForResult(intent, 0);
}

/**
 * onRequestPermissionsResult
 *
 * @param requestCode
 * @param permissions
 * @param grantResults
 */
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == MY_PERMISSIONS_REQUEST_CALL_PHONE) {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // Permission granted
            callMethod();
        } else {
            // Permission Denied
            Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
        }
        return;
    }
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
于 2017-10-28T07:46:26.043 回答