一、camera.open()权限问题
android6.0使用camera.open()时需要在onCreate()里面添加如下代码,否则会报错"Failed to connect to camera service":
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[] {Manifest.permission.CAMERA}, 1);
}
}
二、android摄像头预览,nv21转bitmap
//第一种方式,使用Renderscript,参考https://www.jianshu.com/p/d61443506687
Bitmap bitmap = nv21ToBitmap.nv21ToBitmap(data, framewidth, frameheight);
//第二种方式
/*Mat mat = new Mat(frameheight*3/2,framewidth, CvType.CV_8UC1);//,byteBuffer 1440,1080
int re = mat.put(0,0,data);
Mat bgr_i420 = new Mat(frameheight, framewidth, CvType.CV_8UC3);
Imgproc.cvtColor(mat , bgr_i420, Imgproc.COLOR_YUV2RGBA_NV21);
Bitmap bitmap = Bitmap.createBitmap(bgr_i420.cols(), bgr_i420.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(bgr_i420, bitmap);*/
//第三种方式
/*Bitmap bitmap = null;
try {
YuvImage image = new YuvImage(data, ImageFormat.NV21, 480, 640, null);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
image.compressToJpeg(new android.graphics.Rect(0, 0, 480, 640), 80, stream);
bitmap = BitmapFactory.decodeByteArray(stream.toByteArray(), 0, stream.size());
stream.close();
} catch (IOException e) {
e.printStackTrace();
}*/
三、Android根目录下创建文件夹并保存文件
涉及6.0权限问题,见最后一个url
https://blog.csdn.net/Exception010/article/details/84988941
https://www.cnblogs.com/Sunnor/p/4762457.html
https://blog.csdn.net/Bruce_wenys/article/details/80514797
http://niuti.lofter.com/post/380915_c9e83f2
四、opencv andorid sdk,byte[]与mat互转
//从mat获取byte[]
byte[] bytebuffer = new byte[mat.height() * mat.width()];
mat.get(0,0,bytebuffer);
//从byte创建mat
Mat mat = new Mat(height,width, CvType.CV_8UC1);
int re = mat.put(0,0,data);
五、 查看其它App的AndroidManifest.xml
1.下载AXMLPrinter2.jar
2.将AndroidManifest.xml 放到与AXMLPrinter2.jar的同目录下
3.打开DOS窗口并切换到对应目录,执行命令行: java -jar AXMLPrinter2.jar AndroidManifest.xml >> AndroidManifest.txt
AXMLPrinter2.jar网盘地址
链接:https://pan.baidu.com/s/1_FZRTpqswcyz_NK-DW0aFg?pwd=z22g
提取码:z22g
六、android原生ProgressDialog
dialog = ProgressDialog.show(RecResultActivity.this, "", "正在上传并识别", true);
dialog.dismiss();
七、DatePickerDialog的使用
DatePickerDialog.OnDateSetListener listener=new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker arg0, int year, int month, int day) {
tvShowDialog.setText(year+"-"+(++month)+"-"+day); //将选择的日期显示到TextView中,因为之前获取month直接使用,所以不需要+1,这个地方需要显示,所以+1
}
};
DatePickerDialog dialog=new DatePickerDialog(SettingsActivity.this, 0, listener, year, month, day);//后边三个参数为显示dialog时默认的日期,月份从0开始,0-11对应1-12个月
dialog.show();
八、自定义布局Dialog
自定义一个简单的Dialog布局,包含三个按钮,分别是相机、相册、取消。
Dialog dialog = new Dialog(context);
//加入自定义的布局
View view = View.inflate(context, R.layout.dialog_choise, null);
//加入自定义配置
dialog.setContentView(view);
//设置外部不可以被点击
dialog.setCancelable(false);
Button take = view.findViewById(R.id.btn_take);
Button photo = view.findViewById(R.id.btn_photo);
Button cancenl = view.findViewById(R.id.btn_cancel);
//相机被点击
take.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
int REQUEST_CODE = 1; //事件请求CODE为1
outputImage = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES),"last.jpg");
if (outputImage.exists())
outputImage.delete();
try {
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
imageUri = (Build.VERSION.SDK_INT>=Build.VERSION_CODES.N) ? FileProvider.getUriForFile(context,"com.example.spectrometer.provider",outputImage) : Uri.fromFile(outputImage);
Intent intent1 = new Intent("android.media.action.IMAGE_CAPTURE");
intent1.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent1,REQUEST_CODE);
}
});
//点击相册
photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
int REQUEST_CODE = 2;
Intent intent1 = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent1.addCategory(Intent.CATEGORY_OPENABLE);
intent1.setType("image/*");
startActivityForResult(intent1,REQUEST_CODE);
}
});
//点击取消
cancenl.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
<Button
android:id="@+id/btn_take"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="拍照"/>
<Button
android:id="@+id/btn_photo"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="相册"/>
<Button
android:id="@+id/btn_cancel"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:text="取消"/>
</LinearLayout>
九、 not permitted by network security policy
android27及以上,要求必须使用https。有以下解决办法
(1)APP改用https请求
(2)targetSdkVersion 降到27以下
修改Android 配置中的 compileSdkVersion和targetSdkVersion版本到27以下
(3)更改网络安全配置(推荐使用)
在xml下面新建一个xml文件,比如名称为network_security_config.xml,输入如下内容
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<base-config cleartextTrafficPermitted="true" />
</network-security-config>
在AndroidManifest.xml文件下的application标签增加以下属性
android:networkSecurityConfig="@xml/network_security_config"
十、弹出输入框
final EditText inputServer = new EditText(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setTitle("标题").setIcon(android.R.drawable.ic_dialog_info).setView(inputServer).setNegativeButton("取消", null);
builder.setPositiveButton("保存", null);
AlertDialog alertDialog = builder.create();
alertDialog.show();
Button buttonp = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
LinearLayout.LayoutParams cancelBtnParap = (LinearLayout.LayoutParams) buttonp.getLayoutParams();
cancelBtnParap.weight = 1;
cancelBtnParap.width = LinearLayout.LayoutParams.MATCH_PARENT;
Button buttonn = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
LinearLayout.LayoutParams cancelBtnParan = (LinearLayout.LayoutParams) buttonn.getLayoutParams();
cancelBtnParan.weight = 1;
cancelBtnParan.width = LinearLayout.LayoutParams.MATCH_PARENT;
//这里button进行监听,可以自主控制dialog是否关闭
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//进行自定义操作
alertDialog.dismiss();
}
});
十一、spinner的OnItemSelectedListener被调用两次
解决方法是在事件绑定之前,先设置一个选项。
yuanSpinner.setSelection(0, true);
yuanSpinner.setOnItemSelectedListener(this);
十二、Task 'prepareKotlinBuildScriptModel' not found in project
导入新项目的时候报这个错误,可以考虑在build.gradle文件内增加
task prepareKotlinBuildScriptModel {
}
十三、android项目的多个gradle配置文件的用处
settings.gradle.kts文件(Kotlin)或 settings.gradle文件位于根项目目录中。此设置文件定义项目级存储库设置,并告知 Gradle 在构建应用时应包含哪些模块。多模块项目需要指定应进入最终构建的每个模块。
顶级build.gradle.kts文件(Kotlin)或 build.gradle文件(Groovy)位于根项目目录中。它通常定义项目中模块使用的插件的通用版本。
模块级build.gradle.kts(对于 Kotlin DSL)或 build.gradle文件级(对于 Groovy DSL)位于每个 目录中。它允许您为其所在的特定模块配置构建设置。配置这些构建设置可让您提供自定义打包选项,例如其他构建类型和产品风格,以及覆盖应用清单或顶级构建脚本中的设置。
详细参考官方网站
十四、A problem occurred configuring root project ‘My Application
首先检查gradle版本,如果gradle版本很高,就需要更新的Javav版本。确保选择的 JDK 版本高于或等于您在 Gradle 构建中使用的插件所使用的 JDK 版本。要确定 Android Gradle 插件 (AGP) 所需的最低 JDK 版本。
例如,Android Gradle 插件版本 8.x 需要 JDK 17。如果您尝试运行使用早期版本 JDK 的 Gradle 构建,它会报告如下消息:
An exception occurred applying plugin request [id: 'com.android.application']
> Failed to apply plugin 'com.android.internal.application'.
> Android Gradle plugin requires Java 17 to run. You are currently using Java 11.
Your current JDK is located in /usr/local/buildtools/java/jdk11
You can try some of the following options:
- changing the IDE settings.
- changing the JAVA_HOME environment variable.
- changing `org.gradle.java.home` in `gradle.properties`.
android studio版本和gradle版本对应表。
十五、android 解决错误:Intel HAXM is required to run this AVD
我这里出现这个的主要原因是没有开启Intel Virtualization Technology,原本是开启了的(为了使用Hyper-V 管理器跑虚拟机),后来装了一个腾讯应用宝,这个东西一路把这一些列的东西都给卸载了、关闭了,真是厉害。
只需要进入BIOS界面在“configurations”中找到“Intel Virtualization Technology”将其设置成Enable即可(关于进入BIOS界面的方式不同的电脑大同小异,我的笔记本是联想拯救者,所以我在开机显示联想logo的时候按下F2进入了BIOS界面)。
然后进入android studio重新运行虚拟机,如果还需要装Intel HAXM那就安装一下吧。