Open top menu






Explain:-In this blog explain , How to Get Runtime Permisson( for MarshMallow) in android?

MarshMallowPermission  permission=new MarshMallowPermission(this);

.........................................................................................................................................................



     @Override
   
public void onClick(View v) {
       
switch (v.getId()) {

           
case R.id.SaveButton:
               
permission=new MarshMallowPermission(this);
               
if (!permission.checkPermissionForExternalStorage()){
                   
permission.requestPermissionForExternalStorage();
                }
else {
                        Toast.makeText(MainActivity.
this, "Permission Is Allowed", Toast.LENGTH_SHORT).show();
                    }
                 break;
         }

    }

.........................................................................................................................................................





MarshMallowPermission.Java
Add permission Class ,the class get permission on run Time For MarshMallow.

.........................................................................................................................................................
public class MarshMallowPermission {
   
public static final int RECORD_PERMISSION_REQUEST_CODE = 1;
   
public static final int EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE = 2;
   
public static final int CAMERA_PERMISSION_REQUEST_CODE = 3;
   
public static final int READ_PHONE_STATE_PERMISSION_REQUEST_CODE = 4;
    Activity
activity;

   
public MarshMallowPermission(Activity activity) {
       
this.activity = activity;
    }

   
public boolean checkPermissionForRecord(){
       
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.RECORD_AUDIO);
       
if (result == PackageManager.PERMISSION_GRANTED){
           
return true;
        }
else {
           
return false;
        }
    }

   
public boolean checkPermissionForExternalStorage(){
       
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
       
if (result == PackageManager.PERMISSION_GRANTED){
           
return true;
        }
else {
           
return false;
        }
    }

   
public boolean checkPermissionForCamera(){
       
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);
       
if (result == PackageManager.PERMISSION_GRANTED){
           
return true;
        }
else {
           
return false;
        }
    }

   
public boolean checkPermissionForReadPhoneState(){
       
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE);
       
if (result == PackageManager.PERMISSION_GRANTED){
           
return true;
        }
else {
           
return false;
        }
    }

   
public void requestPermissionForRecord(){
       
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.RECORD_AUDIO)){
            Toast.makeText(
activity, "Microphone permission needed for recording. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
        }
else {
            ActivityCompat.requestPermissions(
activity,new String[]{Manifest.permission.RECORD_AUDIO},RECORD_PERMISSION_REQUEST_CODE);
        }
    }

   
public void requestPermissionForExternalStorage(){
       
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)){
            Toast.makeText(
activity, "External Storage permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
        }
else {
            ActivityCompat.requestPermissions(
activity,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE);
        }
    }

   
public void requestPermissionForCamera(){
       
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.CAMERA)){
            Toast.makeText(
activity, "Camera permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
        }
else {
            ActivityCompat.requestPermissions(
activity,new String[]{Manifest.permission.CAMERA},CAMERA_PERMISSION_REQUEST_CODE);
        }
    }

   
public void requestPermissionReadPhoneState(){
       
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.READ_PHONE_STATE)){
            Toast.makeText(
activity, "Read Phone State permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
        }
else {
            ActivityCompat.requestPermissions(
activity,new String[]{Manifest.permission.READ_PHONE_STATE},READ_PHONE_STATE_PERMISSION_REQUEST_CODE);
        }
    }
}



 .........................................................................................................................................................






 
Read more


Explain:- How to Implement SignatureView in Android?



Open Your build.gradle and make sure that maven entral repository is declared .
repositories {
    jcenter()
    mavenCentral()
}

Then include the library Dependency:
dependencies {
 

   
compile 'com.github.gcacace:signature-pad:1.2.0'
   
}


..................................................................................... 
activity_main.xml

 ..................................................................................... 
xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   
xmlns:tools="http://schemas.android.com/tools"
   
android:id="@+id/activity_main"
   
android:layout_width="match_parent"
   
android:layout_height="match_parent"
   
android:paddingBottom="@dimen/activity_vertical_margin"
   
android:paddingLeft="@dimen/activity_horizontal_margin"
   
android:paddingRight="@dimen/activity_horizontal_margin"
   
android:paddingTop="@dimen/activity_vertical_margin"
   
tools:context="com.project.example.signatureviewdemoproject.MainActivity">

    <
com.github.gcacace.signaturepad.views.SignaturePad
       
android:layout_width="fill_parent"
       
android:layout_height="fill_parent"
       
android:id="@+id/signature_View" />
    <
LinearLayout
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:weightSum="2"
       
android:orientation="horizontal">
        <
Button
           
android:layout_width="match_parent"
           
android:layout_height="match_parent"
           
android:layout_weight="1"
           
android:text="Clear Signature"
           
android:id="@+id/ClearButton"/>

        <
Button
           
android:layout_width="match_parent"
            
android:layout_height="match_parent"
           
android:id="@+id/SaveButton"
           
android:text="Save Signature"
           
android:layout_weight="1"/>
    </
LinearLayout>
</
RelativeLayout>
 ..................................................................................... 

MarshMallowPermission.Java

Add permission Class ,the class get permission on run Time For MarshMallow.

..................................................................................... 

public class MarshMallowPermission {
   
public static final int RECORD_PERMISSION_REQUEST_CODE = 1;
   
public static final int EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE = 2;
   
public static final int CAMERA_PERMISSION_REQUEST_CODE = 3;
   
public static final int READ_PHONE_STATE_PERMISSION_REQUEST_CODE = 4;
    Activity
activity;

   
public MarshMallowPermission(Activity activity) {
       
this.activity = activity;
    }

   
public boolean checkPermissionForRecord(){
       
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.RECORD_AUDIO);
       
if (result == PackageManager.PERMISSION_GRANTED){
           
return true;
        }
else {
           
return false;
        }
    }

   
public boolean checkPermissionForExternalStorage(){
       
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
       
if (result == PackageManager.PERMISSION_GRANTED){
           
return true;
        }
else {
           
return false;
        }
    }

   
public boolean checkPermissionForCamera(){
       
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.CAMERA);
       
if (result == PackageManager.PERMISSION_GRANTED){
           
return true;
        }
else {
           
return false;
        }
    }

   
public boolean checkPermissionForReadPhoneState(){
       
int result = ContextCompat.checkSelfPermission(activity, Manifest.permission.READ_PHONE_STATE);
       
if (result == PackageManager.PERMISSION_GRANTED){
           
return true;
        }
else {
           
return false;
        }
    }

   
public void requestPermissionForRecord(){
       
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.RECORD_AUDIO)){
            Toast.makeText(
activity, "Microphone permission needed for recording. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
        }
else {
            ActivityCompat.requestPermissions(
activity,new String[]{Manifest.permission.RECORD_AUDIO},RECORD_PERMISSION_REQUEST_CODE);
        }
    }

   
public void requestPermissionForExternalStorage(){
       
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)){
            Toast.makeText(
activity, "External Storage permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
        }
else {
            ActivityCompat.requestPermissions(
activity,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},EXTERNAL_STORAGE_PERMISSION_REQUEST_CODE);
        }
    }

   
public void requestPermissionForCamera(){
       
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.CAMERA)){
            Toast.makeText(
activity, "Camera permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
        }
else {
            ActivityCompat.requestPermissions(
activity,new String[]{Manifest.permission.CAMERA},CAMERA_PERMISSION_REQUEST_CODE);
        }
    }

   
public void requestPermissionReadPhoneState(){
       
if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.READ_PHONE_STATE)){
            Toast.makeText(
activity, "Read Phone State permission needed. Please allow in App Settings for additional functionality.", Toast.LENGTH_LONG).show();
        }
else {
            ActivityCompat.requestPermissions(
activity,new String[]{Manifest.permission.READ_PHONE_STATE},READ_PHONE_STATE_PERMISSION_REQUEST_CODE);
        }
    }
}

.....................................................................................  


MainActivity.Java
..................................................................................... 

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
   
private SignaturePad mSignatureView;
    Button
btnSvae, btnClear;
    MarshMallowPermission
permission;
   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);
       
btnSvae = (Button) findViewById(R.id.SaveButton);
       
btnSvae.setOnClickListener(this);
       
btnClear = (Button) findViewById(R.id.ClearButton);
       
btnClear.setOnClickListener(this);
       
mSignatureView = (SignaturePad) findViewById(R.id.signature_View);
       
mSignatureView.setOnSignedListener(new SignaturePad.OnSignedListener() {
           
@Override
           
public void onStartSigning() {

                Toast.makeText(MainActivity.
this, "OnStartSigning", Toast.LENGTH_SHORT).show();
            }

           
@Override
           
public void onSigned() {
                
btnSvae.setEnabled(true);
               
btnClear.setEnabled(true);
            }

           
@Override
           
public void onClear() {
               
btnSvae.setEnabled(false);
               
btnClear.setEnabled(false);
            }
        });
    }

   
@Override
   
public void onClick(View v) {
       
switch (v.getId()) {

           
case R.id.SaveButton:
               
permission=new MarshMallowPermission(this);
               
if (!permission.checkPermissionForExternalStorage()){
                   
permission.requestPermissionForExternalStorage();
                }
else {
                    Bitmap signatureBitmap =
mSignatureView.getSignatureBitmap();
                   
if (addJpgSignatureToGallery(signatureBitmap)) {
                        Toast.makeText(MainActivity.
this, "Signature saved into the Gallery", Toast.LENGTH_SHORT).show();
                    }
else {
                        Toast.makeText(MainActivity.
this, "Unable to store the signature", Toast.LENGTH_SHORT).show();
                    }

                   
if (addSvgSignatureToGallery(mSignatureView.getSignatureSvg())) {
                        Toast.makeText(MainActivity.
this, "SVG Signature saved into the Gallery", Toast.LENGTH_SHORT).show();
                    }
else {
                        Toast.makeText(MainActivity.
this, "Unable to store the SVG signature", Toast.LENGTH_SHORT).show();
                    }
                }
               
break;

           
case R.id.ClearButton:
               
mSignatureView.clear();
               
break;
        }

    }

   
public File getAlbumStorageDir(String albumName) {
       
// Get the directory for the user's public pictures directory.
       
File file = new File(Environment.getExternalStoragePublicDirectory(
                Environment.
DIRECTORY_PICTURES), albumName);
       
if (!file.mkdirs()) {
            Log.e(
"SignaturePad", "Directory not created");
        }
       
return file;
    }

   
public void saveBitmapToJPG(Bitmap bitmap, File photo) throws IOException {
        Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.
ARGB_8888);
        Canvas canvas =
new Canvas(newBitmap);
        canvas.drawColor(Color.
WHITE);
        canvas.drawBitmap(bitmap,
0, 0, null);
        OutputStream stream =
new FileOutputStream(photo);
        newBitmap.compress(Bitmap.CompressFormat.
JPEG, 80, stream);
        stream.close();
    }

   
public boolean addJpgSignatureToGallery(Bitmap signature) {
       
boolean result = false;
       
try {
            File photo =
new File(getAlbumStorageDir("SignaturePad"), String.format("Signature_%d.jpg", System.currentTimeMillis()));
            saveBitmapToJPG(signature, photo);
            scanMediaFile(photo);
            result =
true;
        }
catch (IOException e) {
            e.printStackTrace();
        }
       
return result;
    }

   
private void scanMediaFile(File photo) {
        Intent mediaScanIntent =
new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri contentUri = Uri.fromFile(photo);
        mediaScanIntent.setData(contentUri);
        MainActivity.
this.sendBroadcast(mediaScanIntent);
    }

   
public boolean addSvgSignatureToGallery(String signatureSvg) {
       
boolean result = false;
       
try {
            File svgFile =
new File(getAlbumStorageDir("SignaturePad"), String.format("Signature_%d.svg", System.currentTimeMillis()));
            OutputStream stream =
new FileOutputStream(svgFile);
            OutputStreamWriter writer =
new OutputStreamWriter(stream);
            writer.write(signatureSvg);
            writer.close();
            stream.flush();
            stream.close();
            scanMediaFile(svgFile);
            result =
true;
        }
catch (IOException e) {
            e.printStackTrace();
        }
       
return result;
    }
}
..................................................................................... 


Read more