Android Questions (问题集锦)
来源:互联网 发布:relief f算法 编辑:程序博客网 时间:2024/05/21 00:54
开发过程中,解决问题集锦~
Android Framework Questions
Android External Storage Questions
happen
W/ContextImpl: Failed to ensure directory
不要使用 context.getExternalFilesDir( String type) Api ,顺源码查看,发现此API,存在多个挂载区但不可用时,会打印警告信息。使用 Environment.getExternalStorageDirectory() 则不会
I follow the getExternalFilesDir() source
/** * Ensure that given directories exist, trying to create them if missing. If * unable to create, they are filtered by replacing with {@code null}. */private File[] ensureExternalDirsExistOrFilter(File[] dirs) { File[] result = new File[dirs.length]; for (int i = 0; i < dirs.length; i++) { File dir = dirs[i]; if (!dir.exists()) { if (!dir.mkdirs()) { // recheck existence in case of cross-process race if (!dir.exists()) { // Failing to mkdir() may be okay, since we might not have // enough permissions; ask vold to create on our behalf. final IMountService mount = IMountService.Stub.asInterface( ServiceManager.getService("mount")); try { final int res = mount.mkdirs(getPackageName(), dir.getAbsolutePath()); if (res != 0) { Log.w(TAG, "Failed to ensure " + dir + ": " + res); dir = null; } } catch (Exception e) { Log.w(TAG, "Failed to ensure " + dir + ": " + e); dir = null; } } } } result[i] = dir; } return result;
how to get ExternalFilesDir
public final class StorageUtil {public static final String DIR_ANDROID = "Android";private static final String DIR_DATA = "data";private static final String DIR_FILES = "files";private static final String DIR_CACHE = "cache";@Nullablepublic static synchronized File getExternalStorageAppFilesFile(Context context, String fileName) { if (context == null) return null; if (fileName == null) return null; if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File dirs = buildExternalStorageAppFilesDirs(Environment.getExternalStorageDirectory().getAbsolutePath(), context.getPackageName()); return new File(dirs, fileName); } return null;}public synchronized static File buildExternalStorageAppFilesDirs(String externalStoragePath, String packageName) { return buildPath(externalStoragePath, DIR_ANDROID, DIR_DATA, packageName, DIR_FILES);}public synchronized static File buildPath(String base, String... segments) { File cur = new File(base); for (String segment : segments) { cur = new File(cur, segment); } return cur;}
Android Image Load Libraries
Glide
- happen
Caused by: java.lang.IllegalArgumentException: You cannot start a load for a destroyed activity
加上
Glide.with(context).isPaused()
去调用Glide.with(context).pauseRequests();
即可
public static void resumeRequests(@NonNull final Context context) { if (!isContextDestroyed(context) && isPaused(context)) { Glide.with(context).resumeRequests(); }}public static void pauseRequests(@NonNull final Context context) { if (!isContextDestroyed(context) && !isPaused(context)) { Glide.with(context).pauseRequests(); }}public static boolean isPaused(@NonNull final Context context) { return isContextDestroyed(context) || Glide.with(context).isPaused();}private static boolean isContextDestroyed(@NonNull final Context context) { if (context instanceof Activity) { Activity activity = (Activity) context; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && activity.isDestroyed()) { return true; } } return false;}
Fragment
popBackStack 和 popBackStackImmediate 之坑
在复杂的Fragment 交互中,popBackStack 偶尔抛出java.lang.IllegalStateException Fragment already added
popBackStackImmediate 则偶尔抛出java.lang.IllegalStateException Can not perform this action after onSaveInstanceState
。
跟踪它们源码发现,popBackStack 是Handler异步出栈,popBackStackImmediate 是马上出栈的。我推荐用
popBackStackImmediate,问题来了如何解决上面问题呢
package android.support.v4.app;/** * Created by lazy on 2017/8/23. */public class VendingFragmentManager { /** * make sure no throws java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState ,HA HA * * @param fragmentManager */ public static void allowStateLoss(FragmentManager fragmentManager) { ((FragmentManagerImpl) fragmentManager).noteStateNotSaved(); }}
FragmentManager fragmentManager = getSupportFragmentManager();VendingFragmentManager.allowStateLoss(fragmentManager);fragmentManager.popBackStackImmediate();addMainFragment();
FragmentManagerImpl 类是用默认访问修饰符修饰的,新建android.support.v4.app
包就可以投机取巧访问了。
android studio not find usage
File->Invalidate and Restart
阅读全文
1 0
- Android Questions (问题集锦)
- JSF问题集锦-General Questions
- Android问题集锦(1)
- Android问题集锦之一
- ANDROID 问题集锦
- Android问题集锦_FAQ
- android学习问题集锦
- Android问题集锦
- Android问题集锦
- android开发问题集锦
- android问题集锦
- Android 问题集锦 adb
- Android开发问题集锦
- Android Studio问题集锦
- Android问题集锦
- Android开发问题集锦
- android 问题集锦
- Android开发问题集锦
- Html中的section标签
- Codeforces 416A Guess a number! 题解
- 自学Java之Java语言(Java基础)(001day)
- Java 实例
- Hive-JDBCli
- Android Questions (问题集锦)
- 深入浅出UML类图
- angular做的一个表单带删除排序,搜索等功能
- 梯度下降(cs229笔记)
- Java四舍五入和保留位
- (IDEA)使用struts2后出现启动拦截器失败
- PLSQL 笔记
- UIView的setNeedsLayout, layoutIfNeeded 和 layoutSubviews 方法之间的关系解释
- MAC Windows linux(树莓派)三平台配置 opencv2.4.13 + QT环境