我是android的业余爱好者,我正在尝试从app上传文件到服务器,我需要请求用户的许可,但对话框不会弹出片段。 我有一个 FilePath 类,它有助于获取路径,验证权限方法也在这个类中应该调出权限对话
public class FilePath{
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
@TargetApi(Build.VERSION_CODES.KITKAT)
public static String getPath(final Context context, final Uri uri) {
// check here to KITKAT or new version
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/"
+ split[1];
}
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"),
Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] { split[1] };
return getDataColumn(context, contentUri, selection,
selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
public static String getDataColumn(Context context, Uri uri,
String selection, String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = { column };
try {
cursor = context.getContentResolver().query(uri, projection,
selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
final int index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
public static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri
.getAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri
.getAuthority());
}
public static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri
.getAuthority());
}
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri
.getAuthority());
}
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
Log.d("Permission Checked","Checked permision!!!!!");
ActivityCompat.requestPermissions(activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}
}
然后我在片段类onCreate
中调用此验证权限方法@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_upload, container, false);
ivAttachment = (ImageView) v.findViewById(R.id.ivAttachment);
bUpload = (Button) v.findViewById(R.id.b_upload);
tvFileName = (TextView) v.findViewById(R.id.tv_file_name);
ivAttachment.setOnClickListener(this);
bUpload.setOnClickListener(this);
FilePath.verifyStoragePermissions(this.getActivity());
return v;
}
该片段包含在 MainActivity
上添加的BottomBar
中
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_PERMISSION = 10;
BottomBar mBottomBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBottomBar = BottomBar.attach(this,savedInstanceState);
mBottomBar.setItemsFromMenu(R.menu.menu_main,new OnMenuTabClickListener()
{
public void onMenuTabSelected(@IdRes int i) {
if(i==R.id.Bottombaritemone)
{ NewsFRagment f = new NewsFRagment();
getSupportFragmentManager().beginTransaction().replace(R.id.main_container,f).commit();
}
else if(i==R.id.Bottombaritemtwo)
{
Cardsfragment f =new Cardsfragment();
getSupportFragmentManager().beginTransaction().replace(R.id.main_container,f).commit();
}
else if(i==R.id.Bottombaritemthree)
{
PhoneFragment f =new PhoneFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.main_container,f).commit();
}
else if(i==R.id.Bottombaritemfour)
{
UploadFragment f =new UploadFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.main_container,f).commit();
}
else if(i==R.id.Bottombaritemfive)
{
FavoritesFragment f =new FavoritesFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.main_container,f).commit();
}
}
@Override
public void onMenuTabReSelected(@IdRes int i) {
}
});
mBottomBar.mapColorForTab(0, "#F44336");
mBottomBar.mapColorForTab(1,"#9C27B0");
mBottomBar.mapColorForTab(2,"#03A9F4");
mBottomBar.mapColorForTab(3,"#795548");
mBottomBar.mapColorForTab(4,"#FF6F00");
BottomBarBadge unread;
unread = mBottomBar.makeBadgeForTabAt(3,"#FF0000",13);
unread.show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
我在没有Bottom Bar的情况下对此进行了测试,并且弹出了权限对话框,但我不知道为什么它不在BottomBar
碎片上我试图纠正但是我无法...请求帮助!< / p>
答案 0 :(得分:0)
如果用户拒绝权限,则不会显示权限对话框。在第二次发布时,用户将获得“不再问”#34;防止应用程序在将来询问此权限的选项。
然而,如果用户做了什么事情,那么在UX方面它是非常糟糕的,但没有任何东西可以互动。这种情况也必须处理。在调用requestPermissions之前,您需要检查以显示一个基本原理对话框,说明应用程序需要通过活动的应用程序来获取所请求的权限#ShowShowRequestPermissionRationale
结果是首次请求此权限时将显示合理对话框,如果用户曾将此权限标记为“永不再询问”,则会显示该结果。对于后一种情况,onRequestPermissionsResult将在没有任何权限授予对话框的情况下使用PERMISSION_DENIED进行调用。
我写了一个小gist on github来请求运行时权限。
同时检查此blog以完全了解运行时权限。