I was just trying to write a code to capture an image and save that image in default directory with a name test.jpg. My device do capture the image but it runs the else part of the test case and shows error capturing image.In xml file there is only a button and android:onClick is set to process
public class MainActivity extends 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 requestPermissions
3.Use onRequestPermissionsResult
If permission is granted , you can do something you want ,else you should do requestPermissions
in your code .
If you request permissions,you can deal with result in onRequestPermissionsResult
method .And if permissions granted , you can do something you want ,else deal with that permission Denied .
Try this .
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);
}