Android开发 点滴记录 一
一、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"
相关文章
- Android开发之解决APP启动白屏或者黑屏闪现的问题
- Android开发学习---android下的数据持久化,保存数据到rom文件,android_data目录下文件访问的权限控制
- Android-----View绘制流程以及invalidate()等相关方法分析 .
- Android利用canvas画各种图形 及Paint用法 .
- Android ROM开发(二)——ROM架构以及Updater-Script脚本分析,常见的Status错误解决办法
- EasyPlayer_Android RTSP安卓播放器直播画面卡在第一帧问题修复
- Android开发资料学习(转载/链接)
- Android开发中,那些让你相见恨晚的方法、类或接口
- 《android开发艺术探索》读书笔记(十五)--Android性能优化
- [APP] Android 开发笔记 001-环境搭建与命令行创建项目
- Android开发摆脱行业寒冬必备《Android 音视频开发进阶指南》,开源分享。
- Android KeyEvent事件监听
- Rockchip Android 12.0 SDK开发指南
- Android 网络管理框架
- 【Android笔记87】Android之两种开发模式介绍MVC和MVP(登录小案例源码)
- Linux以及Android开发中的小技巧和长繁命令记录收集
- 使用Eclipse设定Android开发环境
- 七年音视频开发呕心沥血之作:《Android音视频开发进阶指南》开源分享
- Android wifi开发介绍
- Android开发 OkHttp+客户端与服务器