gpt4 book ai didi

android - ActivityCompat.requestPermissions 在 Android 中不显示对话框

转载 作者:行者123 更新时间:2023-11-29 02:37:55 30 4
gpt4 key购买 nike

Android 手机版本:6.0.1 (Android marshmallow)
Android 目标 SDK 版本 25

我在 Android 按钮中调用 ActivityCompat.requestPermissions 但不显示处理它的对话框

按钮

printrequest.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
try{

verifyStoragePermissions(SickFragmentId.this);
}
catch(Exception e ){
Log.e("LoginActv: Save Block", e.getMessage());
e.getStackTrace();
}

String req = textViewReqNo.getText().toString();
String name = Name.getText().toString();
String branch = Branch.getText().toString();
String departement_position = Departement_Position.getText().toString();
String status = Status.getText().toString();
String description = Description.getText().toString();
String type = Status.getText().toString();
String date = Date.getText().toString();

Intent showId = new Intent(SickFragmentId.this, print.class);
startActivity(showId);

}
});

然后我将代码放在 Print.java 中

public class print extends Activity {

private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};

public static void verifyStoragePermissions(Activity activity) {
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

Log.e("Permission value", permission + " ");
if (permission != 1) {
Log.e("Permission ask", "Asking, permission not granted");
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);


if (requestCode == 200) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {

Log.e("PERResult", "Granted");

String FILE = Environment.getExternalStorageDirectory().toString()
+ "/PDF/" + "AldanRIZKISANTOSA.pdf";

Document document = new Document(PageSize.A4);

// Create Directory in External Storage
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/PDF");
myDir.mkdirs();

// Create Pdf Writer for Writting into New Created Document
try {
PdfWriter.getInstance(document, new FileOutputStream(FILE));

// Open Document for Writting into document
document.open();

// User Define Method
addMetaData(document);
addTitlePage(document);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Close Document after writting all content
document.close();

Toast.makeText(this, "PDF File is Created. Location : " + FILE,
Toast.LENGTH_LONG).show();


} else {
Log.e("PERResult", "Denied");
Toast.makeText(getApplicationContext(), "PERMISSION_DENIED", Toast.LENGTH_SHORT).show();
}
}
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

// Set PDF document Properties
public void addMetaData(Document document)

{
document.addTitle("RESUME");
document.addSubject("Person Info");
document.addKeywords("Personal, Education, Skills");
document.addAuthor("TAG");
document.addCreator("TAG");
}

public void addTitlePage(Document document) throws DocumentException {
// Font Style for Document
Font catFont = new Font(Font.FontFamily.TIMES_ROMAN, 18, Font.BOLD);
Font titleFont = new Font(Font.FontFamily.TIMES_ROMAN, 22, Font.BOLD
| Font.UNDERLINE, BaseColor.GRAY);
Font smallBold = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.BOLD);
Font normal = new Font(Font.FontFamily.TIMES_ROMAN, 12, Font.NORMAL);

// Start New Paragraph
Paragraph prHead = new Paragraph();
// Set Font in this Paragraph
prHead.setFont(titleFont);
// Add item into Paragraph
prHead.add("RESUME – Name\n");

// Create Table into Document with 1 Row
PdfPTable myTable = new PdfPTable(1);
// 100.0f mean width of table is same as Document size
myTable.setWidthPercentage(100.0f);

// Create New Cell into Table
PdfPCell myCell = new PdfPCell(new Paragraph(""));
myCell.setBorder(Rectangle.BOTTOM);

// Add Cell into Table
myTable.addCell(myCell);

prHead.setFont(catFont);
prHead.add("\nName1 Name2\n");
prHead.setAlignment(Element.ALIGN_CENTER);

// Add all above details into Document
document.add(prHead);
document.add(myTable);

document.add(myTable);

// Now Start another New Paragraph
Paragraph prPersinalInfo = new Paragraph();
prPersinalInfo.setFont(smallBold);
prPersinalInfo.add("Address 1\n");
prPersinalInfo.add("Address 2\n");
prPersinalInfo.add("City: SanFran. State: CA\n");
prPersinalInfo.add("Country: USA Zip Code: 000001\n");
prPersinalInfo
.add("Mobile: 9999999999 Fax: 1111111 Email: john_pit@gmail.com \n");

prPersinalInfo.setAlignment(Element.ALIGN_CENTER);

document.add(prPersinalInfo);
document.add(myTable);

document.add(myTable);

Paragraph prProfile = new Paragraph();
prProfile.setFont(smallBold);
prProfile.add("\n \n Profile : \n ");
prProfile.setFont(normal);
prProfile
.add("\nI am Mr. XYZ. I am Android Application Developer at TAG.");

prProfile.setFont(smallBold);
document.add(prProfile);

// Create new Page in PDF
document.newPage();
}
}

但是权限对话框不显示

在我使用 ActivityCompat.requestPermissions 之前我有错误

java.io.FileNotFoundException: /storage/emulated/0/PDF/Name.pdf (Permission denied)

在我使用 ActivityCompat.requestPermission 之后我有错误

E/Permission value: -1
E/Permission ask: Asking, permission not granted

最佳答案

if( hasPermissionWriteExternalStorage()){
//do smth
}


public boolean hasPermissionWriteExternalStorage() {
if (Build.VERSION.SDK_INT >= 23) {
if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, PERMISSION_WRITE_EXTERNAL_STORAGE);
return false;
}
} else { //permission is automatically granted on sdk<23 upon installation
return true;
}
}

@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

switch (requestCode) {case PERMISSION_WRITE_EXTERNAL_STORAGE: {
if (getActivity().checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
//do smth
} else {
Toast.makeText(getActivity(), "Access Denied", Toast.LENGTH_SHORT).show();
}
break;
}
}
}

关于android - ActivityCompat.requestPermissions 在 Android 中不显示对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45933131/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com