平时遇到的问题整理

本文整理了在Android开发中遇到的各种问题,包括图片旋转处理、快捷方式创建、ListView设置、WebView弹出键盘、软键盘问题、Activity启动模式与onActivityResult冲突、退出应用策略、权限查询、内存泄漏等,并提供了相应的解决方案。
摘要由CSDN通过智能技术生成

三星图片旋转

独三星的手机拍照之后,你会很清楚的看到会把照片旋转一下,然后你根据路径找到的图片就是已经被旋转的了。处理方式:获取图片的exif(Exchangeable Image File 可交换图像文件)信息中的旋转角度。如果被旋转了,那么就进行逆向选择

public static int readPictureDegree(String path) {
    int degree = 0;
    try {
        ExifInterface exifInterface = new ExifInterface(path);
        int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                degree = 90;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                degree = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                degree = 270;
                break;
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return degree;
}

public static Bitmap toturn(Bitmap img, int degree) {
    Matrix matrix = new Matrix();
    matrix.postRotate(degree);
    int width = img.getWidth();
    int height = img.getHeight();
    img = Bitmap.createBitmap(img, 0, 0, width, height, matrix, true);
    return img;
}

重复创建快捷方式

很多手机厂商取消了快捷方式的概念,导致我们无法通过代码创建一个我们需要的快捷方式

这里写图片描述

对于这些不支持创建快捷方式的手机,咱就忽略了~

Android 的 Launcher 源码在创建快捷方式的时候不仅会判断 duplicate 的值,还会在数据库中查询一下将要被创建的快捷方式是否已经存在,我们也照做就 OK 了

这里写图片描述

但是众多厂商的url不尽相同

2.2 版本以前的URI 是:content://com.android.launcher.settings/favorites?notify=true

2.2~4.3 版本的URI 是:content://com.android.launcher2.settings/favorites?notify=true

4.4 版本以上的目前都是:content://com.android.launcher3.settings/favorites?notify=true

还有特定机型的URL也不同,这里就不列举了~

通过权限查询URI

这里写图片描述

ListView设置分割线与内边距

<?xml version="1.0" encoding="UTF-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
    android:insetLeft="15dp"
    android:insetRight="15dp"
    android:drawable="@color/line_gray">
</inset>
<ListView
     android:id="@+id/listView1"
     android:divider="@drawable/list_item_divider"
     android:dividerHeight="1px"
     android:layout_below="@id/rlHeader1"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"/>

TextView展示小表情

//将发布的评论由Base64编码还原为字符串,为了兼容表情符
if (data.getContent().contains("E5BE88E5B18C")) {
    byte[] decodeBase64 = Base64.decodeBase64(data.getContent().replace("E5BE88E5B18C", ""));
    content.setText(new String(decodeBase64));
} else {
    content.setText(com.emotion.StringUtils.getEmotionContent(MomentsDetailsActivity.this, content, data.getContent()));
}

通过名称获取资源id

int lebId = Resources.getSystem()
            .getIdentifier("permlab_accessNetworkState", 
                "string", "android");
String lab = getString(lebId);

WebView blocks redirect from https to http

Added in API level 21
Used with setMixedContentMode(int) In this mode, the WebView will allow a secure origin to load content from any other origin, even if that origin is insecure. This is the least secure mode of operation for the WebView, and where possible apps should not set this mode.

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
    settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}

弹出软键盘问题

有个需求:比如让你一进入页面就弹出软键盘,网上一搜一堆代码,发现就是弹不出来。为什么?因为可能你的目标view当时还没有完成完整绘制流程

getHandler().postDelayed(new Runnable() {
@Override
public void run() {
        ((InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE))
                .hideSoftInputFromWindow(activity.getWindow().getDecorView().getWindowToken(), 0);
    }
},50);

webview调用js报线程错误

W/WebView(2088): Java.lang.Throwable: A WebView method was called on thread ‘JavaBridge’. All WebView methods must be called on the same thread. (Expected Looper Looper (main, tid 1) {b3dbcb18} called on Looper (JavaBridge, tid 120) {b44a1af8}, FYI main Looper is Looper (main, tid 1) {b3dbcb18})
W/WebView(2088): at android.webkit.WebView.checkThread(WebView.java:2063)
W/WebView(2088): at android.webkit.WebView.loadUrl(WebView.java:794)
W/WebView(2088): at com.ue.oa.activity.XFormActivity.alert(XFormActivity.java:180)
W/WebView(2088): at com.ue.oa.activity.XFormActivity$FormActions.save(XFormActivity.java:193)
W/WebView(2088): at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
W/WebView(2088): at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:27)
W/WebView(2088):

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值