Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

camera permission android

<uses-permission android:name="android.permission.CAMERA" />
Comment

Camera Permission in Android

package edu.gvsu.cis.masl.camerademo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MyCameraActivity extends Activity
{
    private static final int CAMERA_REQUEST = 1888; 
    private ImageView imageView;
    private static final int MY_CAMERA_PERMISSION_CODE = 100;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.imageView = (ImageView)this.findViewById(R.id.imageView1);
        Button photoButton = (Button) this.findViewById(R.id.button1);
        photoButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
                {
                    requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_PERMISSION_CODE);
                }
                else
                {
                    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                    startActivityForResult(cameraIntent, CAMERA_REQUEST);
                } 
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
    {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == MY_CAMERA_PERMISSION_CODE)
        {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
            {
                Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
            else
            {
                Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {  
        if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK)
        {  
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(photo);
        }  
    } 
}
Comment

PREVIOUS NEXT
Code Example
Java :: spring filter exception handling 
Java :: java variables 
Java :: java logger with different colors 
Java :: how to reverse a string in java without function 
Java :: longest common subsequence of two strings 
Java :: boucle for avec un tableau java 
Java :: initialize hashmap with values 
Java :: make textview invisible android 
Java :: java basic syntax 
Java :: render problem in android studio 
Java :: sc.nextline skips 
Java :: quicksort for arraylist 
Java :: comentario java 
Java :: java find if element of list in present in another list 
Java :: java vector push_back 
Java :: multiplication table using while loop in java 
Java :: palindrome java 
Java :: check first character of string is number java 
Java :: Java alt + f4 
Java :: java random number generator in range 
Java :: java get filename without extension 
Java :: mac java version terminal 
Java :: java 2 decimals 
Java :: binary to int java 
Java :: java program to sort an array 
Java :: primefaces download file 
Java :: how to sort arraylist 
Java :: android studio remove button onclick 
Java :: java not equal to 
Java :: java queue implementation 
ADD CONTENT
Topic
Content
Source link
Name
2+9 =