为什么这段从设备中捕获图片的代码不能正常 运行?
Why this code of capturing picture from a device don't run properly?
我只是想写一个代码来捕获图像并将该图像保存在默认目录中,名称为 test.jpg。我的设备确实捕获了图像,但它运行了测试用例的其他部分并显示错误捕获 image.In xml 文件只有一个按钮并且 android:onClick 设置为处理
public class 主要Activity 扩展 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;
}
}
}
}
1.Use checkSelfPermission
2.Use 请求权限
3.Use 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);
}
我只是想写一个代码来捕获图像并将该图像保存在默认目录中,名称为 test.jpg。我的设备确实捕获了图像,但它运行了测试用例的其他部分并显示错误捕获 image.In xml 文件只有一个按钮并且 android:onClick 设置为处理
public class 主要Activity 扩展 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;
}
}
}
}
1.Use checkSelfPermission
2.Use 请求权限
3.Use 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);
}