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 :: java every second 
Java :: java fullscreen jframe 
Java :: spigot broadcast message 
Java :: how to see list of java of versions in mac 
Java :: getcolor deprecated 
Java :: java sleep in code 
Java :: java print stack trace to string 
Java :: priority queue reverse order java 
Java :: javafx get screen size 
Java :: spigot actionbar 
Java :: open link in android studio 
Java :: java reflection get field value 
Java :: flutter Uri toString and String to Uri 
Java :: base64.decode android 
Java :: get intent not working in fragment 
Java :: how to remove all special characters from a string in java 
Java :: printf java boolean 
Java :: kill all java processes windows 
Java :: java bufferedimage get raster data 
Java :: how to delete last char in string java 
Java :: how to check type of primitive value in java 
Java :: java find substring between two strings 
Java :: find difference in days between two dates java 
Java :: big screen jframe 
Java :: from string to double andoird studio 
Java :: jfilechooser file filter 
Java :: string array to stream 
Java :: java swing button on click 
Java :: java int to hex fixed length 
Java :: how to set audio of a java sound clip 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =