Permission dialog box not opening

Asked by At

I am trying to read a file from my sd card. Since I have an android pie, i need to give runtime permissions to access the external storage. The problem is I am not getting the prompt which asks user that the app requests to access files,etc. And the logcat shows permission denied to access the local harddrive.

This is my mainactivity file.

package com.example.vsgan.app1;

import android.Manifest;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.bluetooth.BluetoothAdapter;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Method;
import java.util.Set;
import java.util.UUID;

public class MainActivity extends AppCompatActivity {

View v;
EditText inputText;
TextView outputeText,status;
Button convertBtn,connectBtn,readrqt;
Button bluetoothbtn,readbtn;
String address=null;
OutputStream mmOutputStream;
InputStream mmInputStream;
BluetoothSocket mmSocket;
BluetoothAdapter mBluetoothAdapter;
BluetoothDevice mmDevice;
private static final int PERMISSION_REQUEST_CODE = 100;

char text;
public static final String TAG = "MainActivity";

private ConnectedThread mConnectedThread;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    int REQUEST_ENABLE_BT = 10;
   bluetoothbtn=(Button) findViewById(R.id.ble);
    readbtn=(Button)findViewById(R.id.btn_read);
    readrqt=(Button)findViewById(R.id.readrqt);
    status = (TextView) findViewById(R.id.textView2);
    readrqt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(checkPermission()){
                Toast.makeText(MainActivity.this,"Permission already granted",Toast.LENGTH_SHORT).show();
            }
            else
            {
                requestStoragePermission();
            }

        }
    });
readbtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {


          outputeText.setText(FileHelper.ReadFile(MainActivity.this));


    }
});

    bluetoothbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try {
                findBT();
                openBT();
            } catch (IOException e) {
                Log.d(TAG,"excaption caught");
            }
        }
    });

    inputText = (EditText) findViewById(R.id.editText);
    outputeText = (TextView) findViewById(R.id.textView);
    convertBtn = (Button) findViewById(R.id.btn);

    convertBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int length = inputText.getText().toString().length();
            if (length > 0) {
                for (int i = 0; i < length; i++) {
                    char text = inputText.getText().toString().charAt(i);
                    //brailletotext abc = new brailletotext(length, text);
                    //int convertedtext = abc.convert(length, text);
                    //outputeText.setText(convertedtext);
                    beginListenForData(text);
                }
            }
        }
    });

}
private boolean checkPermission() {
    int result = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE);
    if (result == PackageManager.PERMISSION_GRANTED) {
        return true;
    } else {
        return false;
    }
}
private void requestStoragePermission() {
    if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, android.Manifest.permission.READ_EXTERNAL_STORAGE)) {
        Toast.makeText(MainActivity.this, "Write External Storage permission allows us to read files. Please allow this permission in App Settings.", Toast.LENGTH_LONG).show();
    } else {
        ActivityCompat.requestPermissions(MainActivity.this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE},PERMISSION_REQUEST_CODE);
    }
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults) {
    switch (requestCode) {
        case PERMISSION_REQUEST_CODE:
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            Log.e("value", "Permission Granted, Now you can use local drive .");
        } else {
            Log.e("value", "Permission Denied, You cannot use local drive .");
        }
        break;


    }
}

private void openBT() throws IOException {
    Log.d(TAG,"openBT");
    //UUID uuid = UUID.fromString("00001115-0000-1000-8000-00805F9B34FB");
    UUID uuid = UUID.fromString(String.valueOf(mmDevice.getUuids()[0].getUuid()));
    mmSocket = mmDevice.createRfcommSocketToServiceRecord(uuid);
    mmSocket.connect();
    mConnectedThread = new ConnectedThread(mmSocket);
    mConnectedThread.start();
    mmOutputStream = mmSocket.getOutputStream();
    mmInputStream = mmSocket.getInputStream();

    Log.d(TAG,"Bluetooth Opened");
    //Toast.makeText(getApplicationContext(),"Bluetooth Opened",Toast.LENGTH_SHORT).show();
    status.setText("Bluetooth Opened");
}

private void beginListenForData(char convertedtext) {
    mConnectedThread.write(convertedtext);
    Log.d(TAG,"done");
}

private void findBT() {
    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter==null) {
        Toast.makeText(getApplicationContext(),"No bluetooth adapter is available",Toast.LENGTH_SHORT).show();
        Log.d(TAG,"No bluetooth adapter is available");
    }
    if (!mBluetoothAdapter.isEnabled()) {
        Intent enableBluetooth = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        startActivityForResult(enableBluetooth,0);
    }

    Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
    if (pairedDevices.size()>0) {
        for (BluetoothDevice device : pairedDevices) {
            if (device.getName().equals("HC-05")) {
                mmDevice = device;
                break;
            }
        }
    }
    Log.d(TAG,"Bluetooth device found");
    //Toast.makeText(getApplicationContext(),"Bluetooth device found",Toast.LENGTH_LONG).show();
    status.setText("Bluetooth device found");
    UUID muuid = mmDevice.getUuids()[0].getUuid();
    status.setText(String.valueOf(muuid));
    Log.d(TAG,mmDevice.getName());
}

private class ConnectedThread extends Thread {
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;

    //creation of the connect thread
    public ConnectedThread(BluetoothSocket socket) {
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        try {
            //Create I/O streams for connection
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) { }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    //write method
    public void write(char input) {
       //byte[] msgBuffer =input;         //converts entered String into bytes
        try {
            mmOutStream.write(input);                //write bytes over BT connection via outstream
        } catch (IOException e) {
            //if you cannot write, close the application
            Toast.makeText(getBaseContext(), "Connection Failure", Toast.LENGTH_LONG).show();
            finish();

        }
    }

}

}

This is my androidmanifest.xml file

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vsgan.app1">
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<user-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

This is the logcat error

04-26 15:36:36.008 9723-9723/com.example.vsgan.app1 E/value: Permission Denied, You cannot use local drive .

Please Help me out!!! Thanks in advance

2 Answers

0
Nikunj Sorathiya On Best Solutions

Please add more permission in Runtime: Manifest.permission.WRITE_EXTERNAL_STORAGE

and Manifest File:

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

Add Rutime Permission:

implementation 'gun0912.ted:tedpermission:2.1.0'

In Activity Class

TedPermission.with(getActivity())
                .setPermissionListener(permissionlistener)
                .setDeniedMessage("If you reject permission,you can not use this service\n\nPlease turn on permissions at [Setting] > [Permission]")
                .setPermissions(Manifest.permission.ACCESS_FINE_LOCATION,
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .check();

-

PermissionListener permissionlistener = new PermissionListener() {
        @Override
        public void onPermissionGranted() {

        }

        @Override
        public void onPermissionDenied(ArrayList<String> deniedPermissions) {
        }
    };
0
Aabauser On

You have to use WRITE_EXTERNAL_STORAGE permission also

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

if (ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                        != PackageManager.PERMISSION_GRANTED) {

                    Toast.makeText(context, " Allow the Storage Permission", Toast.LENGTH_LONG).show();
                    ActivityCompat.requestPermissions((Activity) context,
                            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                            MY_PERMISSIONS_CONSTANT);
                    return false;
}