我正在尝试将Gmail文件附加到我的Gmail上。但问题是,它始终显示一条祝愿信息,上面写着“为附件拒绝的许可”。我尝试了一些不同的电子邮件,比如雅虎,但该文件仍未附加。
这是我的附件代码:
case R.id.emailPdf:
try {
String filename = "SampleFile1.pdf";
File filelocation = new File(Environment.getDataDirectory() + "/data/" + getPackageName() + "/files/", filename);
filelocation.setReadable(true, false);
Uri path = Uri.fromFile(filelocation);
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("vnd.android.cursor.dir/email");
String to[] = {""};
emailIntent.putExtra(Intent.EXTRA_EMAIL, to);
emailIntent.putExtra(Intent.EXTRA_STREAM, path);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(Intent.createChooser(emailIntent, "Send email..."), 12);
break;
}catch (Exception e){
e.printStackTrace();
}
这些是AndroidManifest.xml文件中的权限
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
我使用了调试器,我发现文件正在从文件位置读取,但不知道为什么它没有附加。
任何帮助都将不胜感激。
提前致谢。
答案 0 :(得分:0)
尝试如下:
创建检查运行时权限的方法。
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public boolean checkExternalPermission() {
int currentAPIVersion = Build.VERSION.SDK_INT;
if (currentAPIVersion >= android.os.Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) getActivity(), Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(getActivity());
alertBuilder.setCancelable(true);
alertBuilder.setTitle("Permission necessary");
alertBuilder.setMessage("Write external storage permission is necessary to read gallery pictures!!!");
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions((Activity) getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
} else {
ActivityCompat.requestPermissions((Activity) getActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
}
return false;
} else {
return true;
}
} else {
return true;
}
}
现在点击附加按钮:
boolean result = checkExternalPermission();
if (result) {
activeGallery();
}
创建方法以打开图库,如下所示:
/**
* to gallery
*/
private void activeGallery() {
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
现在处理如下所示的权限结果:
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
activeGallery();
} else {
Toast.makeText(getActivity(), "Permission not granted", Toast.LENGTH_LONG).show();
//code for deny
}
break;
}
}
现在为您选择的附件处理如下所示的活动结果:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_LOAD_IMAGE:
if (resultCode == RESULT_OK && null != data) {
try {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver()
.query(selectedImage, filePathColumn, null, null,
null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
// MyImage image = new MyImage();
// image.setTitle("Test");
// image.setDescription(
// "test choose a photo from gallery and add it to " +
// "list view");
// image.setDatetime(System.currentTimeMillis());
File file = new File(picturePath);
Uri imageUri = Uri.fromFile(file);
Product product = new Product(catID, "custom", "#1111", imageUri.toString(), "15000", "30%", 0);
dBhelper.createPRODUCT(product);
imageList.add(0, product);
// imageList.add(0, imageUri);
setimage(imageUri.toString(), imgview, true);
// imageList.add(0, picturePath);
adapter.notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
}
// images.add(image);
}
break;
}
}
最后在全局变量下面声明:
private static final int RESULT_LOAD_IMAGE = 1;
public static final int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 3;
确保您在清单中定义了以下权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />