Android开发一些坑

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq939782569/article/details/82534526

1、关于图片转成base64的问题,请单开一个线程操作,耗时。

public static String imageToBase64(String path){
        if(TextUtils.isEmpty(path)){
            return null;
        }
        InputStream is = null;
        byte[] data = null;
        String result = null;
        try{
            is = new FileInputStream(path);
            data = new byte[is.available()];
            is.read(data);
            result = Base64.encodeToString(data,Base64.DEFAULT);
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            if(null!=is){
                try{
                    is.close();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

2、关于takephoto库的用法,因为兼容做的比较,所以推荐,在给github上搜,第一个就是。用法都有我就不贴了,有一个问题就是ImageView.setUri的问题(或者setImageBitmap())方法在takeSuccess中调用一直显示压缩失败,其实不是压缩失败,而是Android Handler机制的问题。解决办法如下,很简单。

 @Override
    public void takeSuccess(TResult result) {
        super.takeSuccess(result);

        showImg(result.getImage());
    }

    private void showImg(final TImage image) {
        new Thread(){
            @Override
            public void run() {
                path= imageToBase64(image.getCompressPath());
            }
        }.start();
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Bitmap bmp;
                try {
                    bmp = MediaStore.Images.Media.getBitmap(BasicMsgActivity.this.getContentResolver(), Uri.fromFile(new File(image.getCompressPath())));
                    ivUpdate.setImageBitmap(bmp);
                } catch (FileNotFoundException e) {
                } catch (IOException e) {
                }
            }
        });

    }

3、关于jiaozivideo的用法,在模拟器上出现了只有声音没有画面的问题,解决如下,但是在真机上就好了,模拟器过了一会也好了,是视频本身的问题,在web上也出现了这个问题,视频的问题。加上这行代码可能稍微好一些。

<application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:hardwareAccelerated="true"
        android:theme="@style/AppTheme">

4、有时候布局总是预览不出来,换一下Theme会好的。

5、Tablayout让指示器文字下面的线根据文字多少自适应的问题,反射。

public void setTabLine(TabLayout tab){
        try {
            Class<?> tablayout = tab.getClass();
            Field tabStrip = tablayout.getDeclaredField("mTabStrip");
            tabStrip.setAccessible(true);
            LinearLayout ll_tab= (LinearLayout) tabStrip.get(tabLayout);
            for (int i = 0; i < ll_tab.getChildCount(); i++) {
                View child = ll_tab.getChildAt(i);
                child.setPadding(0,0,0,0);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT,1);
                //修改两个tab的间距
                params.setMarginStart(Math.round(getResources().getDimension(R.dimen.x15)));
                params.setMarginEnd(Math.round(getResources().getDimension(R.dimen.x15)));
                child.setLayoutParams(params);
                child.invalidate();
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }

6、关于使用retrofit 网络请求不能跟生命周期绑定的问题,会出现内存泄露,请使用

api 'com.trello.rxlifecycle2:rxlifecycle:2.2.1'
// If you want to use Android Lifecycle for providers
api 'com.trello.rxlifecycle2:rxlifecycle-android-lifecycle:2.2.1'

 具体使用方式为:

mResponseTransformer2 = new ResponseTransformer2();
observable.compose(mResponseTransformer2.handleResult())
        .compose(mResponseTransformer2.applySchedulers())
        .compose(provider.bindToLifecycle())
        .subscribe(subscriber);

provider的创建方式是在activity或者fragment中创建的

protected final LifecycleProvider<Lifecycle.Event> mProvider = AndroidLifecycle.createLifecycleProvider(this);

在BaseActivity与BaseFragment中获取provider对象即可。

猜你喜欢

转载自blog.csdn.net/qq939782569/article/details/82534526