坑记录

420 阅读1分钟

aapt问题

在AndroidStudio  windos的terminal中输入命令 

.\gradlew compileDebug --stacktrace
.\gradlew compileDebugJavaWithJavac
.\gradlew processDebugResources --debug

 (mac os)下输入 

 ./gradlew compileDebug --stacktrace

创建目录问题

创建目录的时候不要用中文 否则可能导致Uri.fromFile(file)的时候被转码导致uri异常!!!

File cameraSavePath ;
private void goCamera() {
    File file = new File(Environment.getExternalStoragePublicDirectory(
        Environment.DIRECTORY_PICTURES),"test");

    //如果file路径中包含中文 将可能导致在Uri转换的时候路径被编码 从而导致异常

    if (!file.exists()) {
        file.mkdirs();    
    }    
    cameraSavePath = new File(file, System.currentTimeMillis() + ".jpg"); 
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);   
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {        
    //第二个参数为 包名.fileprovider        
        uri = FileProvider.getUriForFile(this, "com.demo.fileprovider", cameraSavePath);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);   
     } else {        
        uri = Uri.fromFile(cameraSavePath);//   
    }    
    intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);   
    startActivityForResult(intent, 111);
}

ViewConfiguration 一个UI常量类

ViewConfiguration 这个类是google定义的一些关于UI的大小 距离等一些常量 
方便开发去根据这个判断一些比如 X还是Y滑动

kotlin中使用 by  viewmodel 

添加依赖 

implementation 'androidx.activity:activity-ktx:1.1.0'
implementation 'androidx.fragment:fragment-ktx:1.2.2'

使用

Activity中
 val mViewModel by viewModels<LoginViewModel>()

Fragment中
    //法 1
    val mViewModel by viewModels<LoginViewModel>()
    //法 2
    val mViewModel2 by activityViewModels<LoginViewModel>()

注意事项:

      使用该功能需要设置jvm 1.8 所以需要在gradle中添加

compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

 kotlinOptions {
        jvmTarget = '1.8'
    }

    个人建议把这两个都加上 免得坑

打开PDF文件

1.android N(24) 以上 需要设置 fileprovider

2. 

val file = new File(filePath)
val intent =  Intent(Intent.ACTION_VIEW)
val uri = if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_GRANT_READ_URI_PERMISSION)    FileProvider.getUriForFile(context,"com.包名.fileprovider",file)
}else{
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    Uri.fromFile(file)
}

intent.setDataAndType(uri, "application/pdf")

startActivity(intent)