I am trying to ask for permission to use the Camera and Writing to External Storage.
My Main Activity is:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M)
{
if(checkSelfPermission(Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(MainActivity.this,new String[]{android.Manifest.permission.CAMERA});
}
}
if(ActivityCompat.checkSelfPermission(getApplicationContext(),
Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED)
{
ActivityCompat.requestPermissions(MainActivity.this,new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},REQUEST_PERM_WRITE_STORAGE);
}
else
{
takePhoto(); // calls this method
}
}
});
}
I have included in my Manifest.xml:
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I get this error:
cannot find symbol method requestPermissions(MainActivity,String[])
cannot find symbol method checkSelfPermission(Context,String)
What is it that I am doing wrong. Thanks
For the request camera part try this
final int RequestCameraPermissionID = 1001;
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.CAMERA},RequestCameraPermissionID);
Related
I want to load a pdf file from external storage (Download/Pdfs/myfile.pdf) using AndroidPdfViewer but it shows blank screen without any error. I tried lots of ways but it's not working.
public class PdfViewActivity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File path = new File(Environment.getExternalStorageDirectory().getPath() + "/Download/Pdfs/myfile.pdf");
PDFView pdfView = findViewById(R.id.pdfView);
pdfView.fromFile(path).load();
I have a pdf file in my "Download/Pdfs/myfile.pdf" and i used the above code to load the file but it's not working.
I have given storage permission manually from settings.
Can anyone please correct me where i am making a mistake.
I have tested your code and it works just fine on Android 10 device. Your are missing something from the below:
1.In Android Manifest File add the READ_EXTERNAL_STORAGE permission
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
and inside application tag add requestLegacyExternalStorage to true to be able to have access on External Storage on Android 10 device and above.
<application
android:requestLegacyExternalStorage="true"
2.Verify that the pdf exists on the device under "/Download/Pdfs/myfile.pdf" path.
3.Change your activity using the below code by requesting External Storage permission at runtime first in onCreate method:
public class PdfViewActivity2 extends AppCompatActivity {
private static final int READ_STORAGE_PERMISSION_REQUEST_CODE = 1000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//check if Read External Storage permission was granded
boolean granded = checkPermissionForReadExtertalStorage();
if(!granded){
requestPermissionForReadExtertalStorage();
}
else {
readPdf();
}
}
public boolean checkPermissionForReadExtertalStorage() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int result = checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
return result == PackageManager.PERMISSION_GRANTED;
}
return false;
}
public void requestPermissionForReadExtertalStorage() {
try {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, READ_STORAGE_PERMISSION_REQUEST_CODE);
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public void onRequestPermissionsResult(int requestCode, #NonNull String[] permissions, #NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case READ_STORAGE_PERMISSION_REQUEST_CODE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted. Read Pdf from External Storage
readPdf();
} else {
// permission denied. Disable the functionality that depends on this permission.
}
}
}
}
private void readPdf(){
File path = new File(Environment.getExternalStorageDirectory().getPath() + "/Download/Pdfs/myfile.pdf");
PDFView pdfView = findViewById(R.id.pdfView);
pdfView.fromFile(path).load();
}
}
First, add the library to your build.gradle file
implementation 'com.github.barteksc:android-pdf-viewer:2.8.2'
To open a PDF file from storage, use this code. There are comments that explain what it does.
public class PdfViewActivity2 extends AppCompatActivity {
// Declare PDFView variable
private PDFView pdfView;
private final int PDF_SELECTION_CODE = 99;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize it
pdfView = findViewById(R.id.pdfView);
// Select PDF from storage
// This code can be used in a button
Toast.makeText(this, "selectPDF", Toast.LENGTH_LONG).show();
Intent browseStorage = new Intent(Intent.ACTION_GET_CONTENT);
browseStorage.setType("application/pdf");
browseStorage.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(browseStorage, "Select PDF"), PDF_SELECTION_CODE);
}
// Get the Uniform Resource Identifier (Uri) of your data, and receive it as a result.
// Then, use URI as the pdf source and pass it as a parameter inside this method fromUri(Uri uri)
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PDF_SELECTION_CODE && resultCode == Activity.RESULT_OK && data != null) {
Uri selectedPdfFromStorage = data.getData();
pdfView.fromUri(selectedPdfFromStorage).defaultPage(0).load();
}
}
}
In an Android 10 device your app has no access to external storage.
Unless you add
android:requestLegacyExternalStorage="true"
in application tag of manifest file.
Instead of using fromFile() use fromSource(). i.e. Declare pathe as DocumentSource instead of File.
public class PdfViewActivity2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DocumentSource path = new File(Environment.getExternalStorageDirectory().getPath() + "/Download/myfile.pdf");
PDFView pdfView = findViewById(R.id.pdfView);
pdfView.fromSource(path).load();
I tried to test my GPS permission on my app .
I have built permission manager and added GPS permission in the manifest because GPS is a dangerous permission.
However, when I run my app I cannot see any dialog box for this permission,
because of that the app toast me that I haven't allowed this permission even though I never have the chance to allow or block this permission on the dialog box.
Would appreciate any kind of help,those are my classes:
Manifest permission
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
MainActiviey
public class MainActivity extends AppCompatActivity {
public static final int GPS=1;
public static final String gpsFinePermission="Manifest.permission.ACCESS_FINE_LOCATION";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PermissionManager.check(MainActivity.this, gpsFinePermission, GPS);
System.out.println("1");
}
#Override
protected void onStart() {
//startService(new Intent(this,CurrentLocation.class));
super.onStart();
System.out.println("2");
}
#Override//when user allowed OR denied a permission
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
System.out.println("3");
if(grantResults[0] == PackageManager.PERMISSION_GRANTED && requestCode == GPS){//Allowed
Toast.makeText(MainActivity.this, "GPS permission granted", Toast.LENGTH_LONG).show();
System.out.println("4");
}else
Toast.makeText(MainActivity.this, "GPS permission is IMPORTANT for this app", Toast.LENGTH_LONG).show();
}
}
Permission manager
public class PermissionManager {
//A method that can be called from any Activity, to check for specific permission
public static void check(Activity activity, String permission, int requestCode){
System.out.println("5");
//If requested permission isn't Granted yet
if (ActivityCompat.checkSelfPermission(activity, permission) != PackageManager.PERMISSION_GRANTED) {
System.out.println("6");
//Request permission from user
ActivityCompat.requestPermissions(activity,new String[]{permission},requestCode);
}
}
}
You should get a string from Manifest.permission class. But you are passing Manifest.permission.ACCESS_FINE_LOCATION as String.
You should not use this.
public static final String gpsFinePermission="Manifest.permission.ACCESS_FINE_LOCATION";
Use this.
public static final String gpsFinePermission= Manifest.permission.ACCESS_FINE_LOCATION;
I am using Vungle SDK 5.1.0.
In Manifest:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
[...]
<activity android:name="com.vungle.publisher.VideoFullScreenAdActivity"
android:configChanges="keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"/>
<activity android:name="com.vungle.publisher.MraidFullScreenAdActivity"
android:configChanges="keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize"
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen"/>
<activity android:name="com.vungle.publisher.FlexViewAdActivity"
android:configChanges="keyboardHidden|orientation|screenSize|screenLayout|smallestScreenSize"
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen"/>
In my first Activity:
final VunglePub vunglePub = VunglePub.getInstance();
[...]
vunglePub.init(this, "XXXXX", new String[] { "PLACEMEXXXXX" }, new VungleInitListener() {
#Override
public void onSuccess() {
}
#Override
public void onFailure(Throwable e){
}
});
[...]
#Override
protected void onPause() {
super.onPause();
vunglePub.onPause();
}
#Override
protected void onResume() {
super.onResume();
vunglePub.onResume();
}
In my Main-Activity:
final VunglePub vunglePub = VunglePub.getInstance();
final AdConfig globalAdConfig = vunglePub.getGlobalAdConfig();
[...]
#Override
protected void onPause() {
super.onPause();
vunglePub.onPause();
}
#Override
protected void onResume() {
super.onResume();
vunglePub.onResume();
}
If I try to call
vunglePub.playAd("PLACEMEXXXXX", globalAdConfig);
I get noting. No Ads. And ideas how I can resolve my problem?
I am Gabor from Vungle. Is your app set to test mode in our dashboard? If yes, please switch to active mode and try again. Give it 30 minutes to be sure the app is really in active mode.
If you app is in active mode already, please contact us at tech-support#vungle.com with your Vungle APP ID and placement ID, so we can have a deeper look for you.
Thanks
Gabor
Hi I'm having a problem while making an app on Android Studio. I want the user to input some text and then click the save button, which should save the text file on my phone. I have the following code running without any errors, however it is not doing anything. I cant find the file that it is supposed to save in.
public class TakeNotes extends Activity implements View.OnClickListener {
String content = "";
File file;
FileOutputStream outputStream;
TextView tv;
Button btnSave;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.take_notes);
tv = (TextView) findViewById(R.id.txtInput);
btnSave = (Button) findViewById(R.id.btnSave);
btnSave.setOnClickListener(this);
}
public void onClick(View v) {
if (v == findViewById(R.id.btnSave)) {
try {
file = new File(Environment.getExternalStorageDirectory(), "test.txt");
outputStream = new FileOutputStream(file);
outputStream.write(content.getBytes());
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
You need to add runtime permission in Android 6.0 (API Level 23) and up
This is the code for WRITE_EXTERNAL_STORAGE
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG,"Permission is granted");
return true;
}
Ask for permission else like this
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
Here is the official docs
You should include permissions in your AndroidManifest :
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I am trying to make an on click button which makes phone calls when pressed.
Here is my code for java:
public void CampusSafClick(View view){
Intent callIntent =new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:6038994210"));
startActivity(callIntent);
}
I understand how to make onclick buttons, so that is not the issue.
I have this code in the manifest:
<uses-permission android:name="android.permision.CALL_PHONE"></uses-permission>
I keep getting the error Unfortunately your app has stop working.
Here is the working code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialContactPhone("123123123");
}
});
}
private void dialContactPhone(final String phoneNumber) {
startActivity(new Intent(Intent.ACTION_DIAL, Uri.fromParts("tel", phoneNumber, null)));
}
You need Action_Dial,
use below code it will open Dealer with number specified.
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:0123456789"));
startActivity(intent);
The tel: prefix is required, otherwhise the following exception will be thrown: java.lang.IllegalStateException: Could not execute method of the activity.
Action_Dial doesn't require any permission.
If you want to initiate the call directly without user's interaction, you can use action Intent.ACTION_CALL. In this case, you must add the following permission in your AndroidManifest.xml:
<uses-permission android:name="android.permission.CALL_PHONE" />
I need to enter code above the application list on the manifest:
<uses-permission android:name="android.permission.CALL_PHONE"/>
You can use the following code
intent =new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:+251999999999"));
startActivity(intent);
and include in manifest file
<uses-permission android:name="android.permission.CALL_PHONE"/>
ivCall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (serviceListDates.get(position).getUser_mobile().length() != 0) {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);
alertDialog.setTitle("NKA SERVICE");
alertDialog.setMessage("Do you want to Call ?");
alertDialog.setIcon(R.drawable.call_icon);
alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
((DeliveredServiceOilActivity) mContext).callPhoneNumber
(serviceListDates.get(position).getUser_mobile());
}
});`enter code here
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.show();
} else
AlertUtils.SHOW_TOAST(mContext, mContext.getString(R.string.please_add_number));
}
});
For make a call using android, It can be implement using Intents.
public void MakePhoneCall(View view){
Intent callIntent =new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:9961907453"));
if (ActivityCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
return;
}
startActivity(callIntent);
}
I have this code in the manifest:
<uses-permission android:name="android.permision.CALL_PHONE"></uses-permission>
If you are using SDK version greater than Lolipop then you should include request permission.