文章目录
- 前言
- 一、抓取这些玩意需要的权限
- 1.kotlin
- 2.Java
- 二、获取手机短信
- 1.kotlin
- 2.Java
- 三、获取手机所有应用信息
- 1.kotlin
- 2.Java
- 三、获取通讯录
- 1.获取通讯录工具类
- 2.MessageBean
- 3.kotlin引用
- 4.Java引用
- 四、通话记录
- 1.LogBean
- 2.kotlin引用
- 3.Java引用
- 五、系统相册
- 1.ImagBean
- 2.kotlin
- 3.Java
- 4.将时间转换为时间戳
- 5.截取特定字符前后方法
- 六、JSON字符串写成文件
- 1.kotlin
- 2.Java
- 3.引用
- 总结
前言
一般我们做金融APP会遇到风控需求,需要获取用户手机短信,手机所有安装应用信息,通讯录,通话记录等功能,接下来我们看看怎么做,一篇文章解决所有!
一、抓取这些玩意需要的权限
1.kotlin
//6.0才用动态权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (activity?.let {
ContextCompat.checkSelfPermission(
it,
Manifest.permission.READ_SMS
)
}
!= PackageManager.PERMISSION_GRANTED
) {
// 申请读写内存卡内容和拍照 获取短信,获取通话记录,获取通讯录权限
activity?.let {
ActivityCompat.requestPermissions(
it, arrayOf(
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_SMS,
Manifest.permission.READ_CALL_LOG,
Manifest.permission.READ_CONTACTS
), 1
)
}
} else {
startActivity(Intent(activity, UotyActivity::class.java))
}
}
2.Java
//6.0才用动态权限
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_SMS)
!= PackageManager.PERMISSION_GRANTED) {
// 申请读写内存卡内容和拍照 获取短信,获取通话记录,获取通讯录权限
ActivityCompat.requestPermissions(getActivity(),
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_SMS, Manifest.permission.READ_CALL_LOG, Manifest.permission.READ_CONTACTS}, 1);
} else {
startActivity(new Intent(getActivity(), UotyActivity.class));
}
}
二、获取手机短信
1.kotlin
/**
* 获取通短信
*/
@SuppressLint("Range")
private fun getSMS() {
val uri = Uri.parse("content://sms/")
val projection = arrayOf("_id", "address", "person", "body", "date", "type")
val cursor = contentResolver.query(uri, projection, null, null, "date desc")
if (cursor != null && cursor.moveToFirst()) {
do {
val bean = SMSBean()
val id = cursor.getString(cursor.getColumnIndex("_id")) //id
val address = cursor.getString(cursor.getColumnIndex("address")) //电话
val person = cursor.getString(cursor.getColumnIndex("person")) //姓名
val body = cursor.getString(cursor.getColumnIndex("body")) //内容
val date = cursor.getString(cursor.getColumnIndex("date")) //时间戳
val type = cursor.getString(cursor.getColumnIndex("type")) //类型1为收短信,2为发短信
var name: String = getContactNameFromNumber(address) + ""
var phoneNumber: String = getContactPhoneNumberFromNumber(address) + ""
if (name == "" || name == "null") {
name = address
}
if (phoneNumber == "" || phoneNumber == "null") {
phoneNumber = name
}
println("获取短信信息:id: $id, name: $name,phone:$phoneNumber, person: $person, body: $body, date: $date, type: $type")
bean.id = id
bean.name = name
bean.person = person
bean.body = body
bean.date = date
bean.phone = phoneNumber
bean.type = type
//这里的smsBeanList是一个MutableList<SMSBean>可变集合
smsBeanList.add(bean)
} while (cursor.moveToNext())
}
cursor?.close()
//这里JSON字符串是给后台的
json = Gson().toJson(smsBeanList)
println("获取短信信息json:$json")
}
2.Java
/**
* 获取通短信
*/
@SuppressLint("Range")
private void getSMS() {
Uri uri = Uri.parse("content://sms/");
String[] projection = new String[]{"_id", "address", "person", "body", "date", "type"};
Cursor cursor = getContentResolver().query(uri, projection, null, null, "date desc");
if (cursor != null && cursor.moveToFirst()) {
do {
SMSBean bean = new SMSBean();
String id = cursor.getString(cursor.getColumnIndex("_id"));//id
String address = cursor.getString(cursor.getColumnIndex("address"));//电话
String person = cursor.getString(cursor.getColumnIndex("person"));//姓名
String body = cursor.getString(cursor.getColumnIndex("body"));//内容
String date = cursor.getString(cursor.getColumnIndex("date"));//时间戳
String type = cursor.getString(cursor.getColumnIndex("type"));//类型1为收短信,2为发短信
String name = getContactNameFromNumber(address) + "";
String phoneNumber = getContactPhoneNumberFromNumber(address) + "";
if (name.equals("") || name.equals("null")) {
name = address;
}
if (phoneNumber.equals("") || phoneNumber.equals("null")) {
phoneNumber = name;
}
System.out.println("获取短信信息:" + "id: " + id + ", name: " + name + ",phone:" + phoneNumber + ", person: " + person + ", body: " + body + ", date: " + date + ", type: " + type);
bean.setId(id);
bean.setName(name);
bean.setPerson(person);
bean.setBody(body);
bean.setDate(date);
bean.setPhone(phoneNumber);
bean.setType(type);
//这里的smsBeanList是一个List<SMSBean>集合
smsBeanList.add(bean);
} while (cursor.moveToNext());
}
if (cursor != null) {
cursor.close();
}
//这里JSON字符串是给后台的
json = new Gson().toJson(smsBeanList);
System.out.println("获取短信信息json:" + json);
三、获取手机所有应用信息
1.kotlin
// 获取已安装的应用信息队列
@SuppressLint("WrongConstant")
fun getAppInfo(ctx: Context, type: Int): MutableList<AppInfo> {
val appList: ArrayList<AppInfo> = ArrayList<AppInfo>()
val siArray = SparseIntArray()
// 获得应用包管理器
val pm = ctx.packageManager
// 获取系统中已经安装的应用列表
val installList = pm.getInstalledApplications(
PackageManager.PERMISSION_GRANTED
)
for (i in installList.indices) {
val item = installList[i]
// 去掉重复的应用信息
if (siArray.indexOfKey(item.uid) >= 0) {
continue
}
// 往siArray中添加一个应用编号,以便后续的去重校验
siArray.put(item.uid, 1)
try {
// 获取该应用的权限列表
val permissions = pm.getPackageInfo(
item.packageName, PackageManager.GET_PERMISSIONS
).requestedPermissions ?: continue
var isQueryNetwork = false
for (permission in permissions) {
// 过滤那些具备上网权限的应用
if (permission == "android.permission.INTERNET") {
isQueryNetwork = true
break
}
}
// 类型为0表示所有应用,为1表示只要联网应用
if (type == 0 || type == 1 && isQueryNetwork) {
try {
val packageInfo = pm.getPackageInfo(packageName, 0)
//应用装时间
firstInstallTime = packageInfo.firstInstallTime.toString() + ""
//应用最后一次更新时间
val lastUpdateTime = packageInfo.lastUpdateTime
} catch (e: PackageManager.NameNotFoundException) {
e.printStackTrace()
}
val app = AppInfo()
app.setUid(item.uid.toString() + "") // 获取应用的编号
app.setPackagename(item.packageName)
app.setName(item.loadLabel(pm).toString())
app.setTime(firstInstallTime)
appList.add(app)
}
} catch (e: java.lang.Exception) {
e.printStackTrace()
continue
}
}
return appList // 返回去重后的应用包队列
}
//这里得到JSON字符串appInfoList是可变集合MutableList<AppInfo>
appInfoList = getAppInfo(this@UotyActivity, 0)
json = Gson().toJson(appInfoList)
2.Java
// 获取已安装的应用信息队列
public ArrayList<AppInfo> getAppInfo(Context ctx, int type) {
ArrayList<AppInfo> appList = new ArrayList<AppInfo>();
SparseIntArray siArray = new SparseIntArray();
// 获得应用包管理器
PackageManager pm = ctx.getPackageManager();
// 获取系统中已经安装的应用列表
@SuppressLint("WrongConstant")
List<ApplicationInfo> installList = pm.getInstalledApplications(
PackageManager.PERMISSION_GRANTED);
for (int i = 0; i < installList.size(); i++) {
ApplicationInfo item = installList.get(i);
// 去掉重复的应用信息
if (siArray.indexOfKey(item.uid) >= 0) {
continue;
}
// 往siArray中添加一个应用编号,以便后续的去重校验
siArray.put(item.uid, 1);
try {
// 获取该应用的权限列表
String[] permissions = pm.getPackageInfo(item.packageName,
PackageManager.GET_PERMISSIONS).requestedPermissions;
if (permissions == null) {
continue;
}
boolean isQueryNetwork = false;
for (String permission : permissions) {
// 过滤那些具备上网权限的应用
if (permission.equals("android.permission.INTERNET")) {
isQueryNetwork = true;
break;
}
}
// 类型为0表示所有应用,为1表示只要联网应用
if (type == 0 || (type == 1 && isQueryNetwork)) {
try {
PackageInfo packageInfo = pm.getPackageInfo(getPackageName(), 0);
//应用装时间
firstInstallTime = packageInfo.firstInstallTime + "";
//应用最后一次更新时间
long lastUpdateTime = packageInfo.lastUpdateTime;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
AppInfo app = new AppInfo();
app.setUid(item.uid + ""); // 获取应用的编号
app.setPackagename(item.packageName);
app.setName(item.loadLabel(pm).toString());
app.setTime(firstInstallTime);
appList.add(app);
}
} catch (Exception e) {
e.printStackTrace();
continue;
}
}
return appList; // 返回去重后的应用包队列
}
//这里得到JSON字符串appInfoList是集合List<AppInfo>
appInfoList = getAppInfo(UotyActivity.this, 0);
json = new Gson().toJson(appInfoList);
三、获取通讯录
1.获取通讯录工具类
/**
* @Author : CaoLiulang
* @Time : 2023/7/10 09:13
* @Description :获取联系人工具类
*/
public class ContactUtils {
private final Context context;
private List<MessageBean> contactData;
public ContactUtils(Context context) {
this.context = context;
}
//获取联系人所有信息(这里返回String,你也可以直接返回其他类型改改就可以了)
@SuppressLint("Range")
public List<MessageBean> getInformation() throws JSONException {
contactData = new ArrayList<>();
int num = 0;
// 获得所有的联系人
Cursor cur = context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
// 循环遍历
if (cur.moveToFirst()) {
int idColumn = cur.getColumnIndex(ContactsContract.Contacts._ID);
int displayNameColumn = cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
do {
MessageBean messageBean = new MessageBean();
num++;
// 获得联系人的ID号
String contactId = cur.getString(idColumn);
// 获得联系人姓名
String disPlayName = cur.getString(displayNameColumn);
// 查看该联系人有多少个电话号码。如果没有这返回值为0
int phoneCount = cur.getInt(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
messageBean.setDisplayName(disPlayName);
messageBean.setId(contactId);
if (phoneCount > 0) {
// 获得联系人的电话号码
Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
if (phones.moveToFirst()) {
do {
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
messageBean.setPhoneNumber(phoneNumber);
} while (phones.moveToNext());
}
// 获取该联系人邮箱
Cursor emails = context.getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
if (emails.moveToFirst()) {
do {
String emailValue = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
messageBean.setEmail(emailValue);
} while (emails.moveToNext());
}
contactData.add(messageBean);
}
} while (cur.moveToNext());
}
Log.e("联系人信息=====", contactData.toString());
return contactData;
}
}
2.MessageBean
这里我只需要这些字段,需要更多自己添加
public class MessageBean {
private String id;
private String phoneNumber;
private String displayName;
private String email;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getPhoneNumber() {
return phoneNumber;
}
public void setPhoneNumber(String phoneNumber) {
this.phoneNumber = phoneNumber;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
3.kotlin引用
//实例化工具类
ContactUtils mobiletype = ContactUtils(this)
//list为MutableList<MessageBean>
list = mobiletype.information
json = Gson().toJson(list)
4.Java引用
//实例化工具类
ContactUtils mobiletype = new ContactUtils(this);
//list为List<MessageBean>
list = mobiletype.getInformation();
json = new Gson().toJson(list);
四、通话记录
1.LogBean
public class LogBean {
public String id;
public String name;
public String number;
public String date;
public String duration;
public String type;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getDuration() {
return duration;
}
public void setDuration(String duration) {
this.duration = duration;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "LogBean{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", number='" + number + '\'' +
", date='" + date + '\'' +
", duration='" + duration + '\'' +
", type='" + type + '\'' +
'}';
}
}
2.kotlin引用
/**
* 获取通话记录
*/
@SuppressLint("Range")
private fun getLog() {
val uri = CallLog.Calls.CONTENT_URI
val projection = arrayOf(
CallLog.Calls._ID,
CallLog.Calls.CACHED_NAME,
CallLog.Calls.NUMBER,
CallLog.Calls.DATE,
CallLog.Calls.DURATION,
CallLog.Calls.TYPE
)
val cursor =
contentResolver.query(uri, projection, null, null, CallLog.Calls.DEFAULT_SORT_ORDER)
if (cursor != null && cursor.moveToFirst()) {
do {
val bean = LogBean()
val id =
cursor.getString(cursor.getColumnIndex(CallLog.Calls._ID))
val number =
cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER))
val name =
cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME))
val date =
cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE))
val duration =
cursor.getString(cursor.getColumnIndex(CallLog.Calls.DURATION))
val type =
cursor.getString(cursor.getColumnIndex(CallLog.Calls.TYPE))
bean.setId(id)
bean.setName(name)
bean.setDate(date)
bean.setType(type)
bean.setNumber(number)
bean.setDuration(duration)
logbean.add(bean)
println("获取通话记录:id: $id,name$name, number: $number, date: $date, duration: $duration, type: $type")
} while (cursor.moveToNext())
}
cursor?.close()
//logbean为可变集合MutableList<LogBean>
json = Gson().toJson(logbean)
println("上传文件JSON:$json")
}
3.Java引用
/**
* 获取通话记录
*/
@SuppressLint("Range")
private void getLog() {
Uri uri = CallLog.Calls.CONTENT_URI;
String[] projection = new String[]{CallLog.Calls._ID, CallLog.Calls.CACHED_NAME, CallLog.Calls.NUMBER, CallLog.Calls.DATE, CallLog.Calls.DURATION, CallLog.Calls.TYPE};
Cursor cursor = getContentResolver().query(uri, projection, null, null, CallLog.Calls.DEFAULT_SORT_ORDER);
if (cursor != null && cursor.moveToFirst()) {
do {
LogBean bean = new LogBean();
String id = cursor.getString(cursor.getColumnIndex(CallLog.Calls._ID));
String number = cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER));
String name = cursor.getString(cursor.getColumnIndex(CallLog.Calls.CACHED_NAME));
String date = cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE));
String duration = cursor.getString(cursor.getColumnIndex(CallLog.Calls.DURATION));
String type = cursor.getString(cursor.getColumnIndex(CallLog.Calls.TYPE));
bean.setId(id);
bean.setName(name);
bean.setDate(date);
bean.setType(type);
bean.setNumber(number);
bean.setDuration(duration);
//logbean为集合List<LogBean>
logbean.add(bean);
System.out.println("获取通话记录:" + "id: " + id + ",name" + name + ", number: " + number + ", date: " + date + ", duration: " + duration + ", type: " + type);
} while (cursor.moveToNext());
}
if (cursor != null) {
cursor.close();
}
json = new Gson().toJson(logbean);
}
五、系统相册
1.ImagBean
public class ImagBean {
private String url;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
@Override
public String toString() {
return "ImagBean{" +
"url='" + url + '\'' +
'}';
}
}
2.kotlin
/**
* 获取系统相册图片
*/
@SuppressLint("Range")
private fun getImag() {
val mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI
// 获得图片
val mCursor = contentResolver.query(
mImageUri,
null,
MediaStore.Images.Media.MIME_TYPE + "=? or " + MediaStore.Images.Media.MIME_TYPE + "=?",
arrayOf("image/jpeg", "image/png"),
MediaStore.Images.Media.DATE_MODIFIED
)
val count = mCursor!!.count
println("图片数量总量:$count")
var sl = 0
if (count > 0) {
while (mCursor.moveToNext()) {
val path =
mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DATA)) // 路径 imageView.setImageURI(Uri.parse(path));
val name =
mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME))
val exif = ExifInterface(path)
val date = exif.getAttribute(ExifInterface.TAG_DATETIME) + "" //时间
if (date == "" || date == "null") {
} else {
val yer: String = getStrBefore(date)
val yer1 = yer.replace(":".toRegex(), "-")
val da: String = getStrAfter(date)
val zzstr = "$yer1 $da"
val rigtime = Date().time - 24 * 30 * 60 * 60 * 1000L
println("图片时间打印:" + rigtime + " " + zzstr + " " + dateToStamp(zzstr) + " " + Date().time)
if (dateToStamp(zzstr)!! > rigtime) { //一个月内的图片
sl++
//只取90张图片
if (imagBeanList.size < 90) {
// //imgPath就是压缩后的图片
// String imgPath = BitmapUtil.compressImageUpload(path);
val bean = ImagBean()
bean.url = path
//imagBeanList为 MutableList<ImagBean>
imagBeanList.add(bean)
println("图片路径打印:$path---->$name")
}
}
}
}
println("图片数量一个月内图片数量:$sl")
println("图片数量--->:" + imagBeanList.size)
val imagjson = Gson().toJson(imagBeanList)
println("上传文件imagBeanList数组打印:$imagjson")
}
}
3.Java
/**
* 获取系统相册图片
*/
private void getImag() throws IOException, java.text.ParseException {
Uri mImageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
// 获得图片
Cursor mCursor = getContentResolver().query(mImageUri, null,
MediaStore.Images.Media.MIME_TYPE + "=? or "
+ MediaStore.Images.Media.MIME_TYPE + "=?",
new String[]{"image/jpeg", "image/png"}, MediaStore.Images.Media.DATE_MODIFIED);
int count = mCursor.getCount();
System.out.println("图片数量总量:" + count);
int sl = 0;
if (count > 0) {
while (mCursor.moveToNext()) {
@SuppressLint("Range") String path = mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DATA));// 路径 imageView.setImageURI(Uri.parse(path));
@SuppressLint("Range") String name = mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME));
ExifInterface exif = new ExifInterface(path);
String date = exif.getAttribute(ExifInterface.TAG_DATETIME) + "";//时间
if (date.equals("") || date.equals("null")) {
} else {
String yer = getStrBefore(date);
String yer1 = yer.replaceAll(":", "-");
String da = getStrAfter(date);
String zzstr = yer1 + " " + da;
long rigtime = new Date().getTime() - 24 * 30 * 60 * 60 * 1000L;
System.out.println("图片时间打印:" + rigtime + " " + zzstr + " " + dateToStamp(zzstr) + " " + new Date().getTime());
if (dateToStamp(zzstr) > rigtime) {//一个月内的图片
sl++;
if (imagBeanList.size() < 90) {
// //imgPath就是压缩后的图片
// String imgPath = BitmapUtil.compressImageUpload(path);
ImagBean bean = new ImagBean();
bean.setUrl(path);
//这里的imagBeanList为List<ImagBean>
imagBeanList.add(bean);
System.out.println("图片路径打印:" + path + "---->" + name);
}
}
}
}
System.out.println("图片数量一个月内图片数量:" + sl);
System.out.println("图片数量--->:" + imagBeanList.size());
String imagjson = new Gson().toJson(imagBeanList);
System.out.println("imagBeanList数组打印:" + imagjson);
}
}
4.将时间转换为时间戳
1.kotlin
@Throws(android.net.ParseException::class, ParseException::class)
fun dateToStamp(time: String?): Long? {
val simpleDateFormat = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
val date = simpleDateFormat.parse(time)
return date.time
}
2.java
public Long dateToStamp(String time) throws ParseException, java.text.ParseException {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = simpleDateFormat.parse(time);
long ts = date.getTime();
return ts;
}
5.截取特定字符前后方法
1.kotlin
//截取字符串前面文本
private fun getStrBefore(query: String): String {
val split = query.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
return split[0]
}
//截取字符串后面文本
private fun getStrAfter(query: String): String {
val split = query.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
return split[1]
}
2.Java
//截取字符串前面文本
private String getStrBefore(String query) {
String[] split = query.split(" ");
String result = split[0];
return result;
}
//截取字符串后面文本
private String getStrAfter(String query) {
String[] split = query.split(" ");
String result = split[1];
return result;
}
六、JSON字符串写成文件
把获取到这些转为JSON字符串后写入文件,把文件传给后台
1.kotlin
fun WriteSd(str: String): File? {
println("字符串打印:$str")
try {
val dirPath =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).path + "/contact"
val dir = File(dirPath)
if (!dir.exists()) {
//创建目录
dir.mkdirs()
}
val file = File(dir.canonicalPath + "/" + "contact.json")
if (file.exists()) {
file.delete()
}
file.createNewFile()
val out = FileOutputStream(file)
val outputStreamWrite = OutputStreamWriter(out, "utf-8")
outputStreamWrite.write(str)
outputStreamWrite.close()
out.close()
println("文件写入成功:" + file.canonicalPath)
return file
} catch (e: java.lang.Exception) {
println("文件写入失败:" + e.message)
e.printStackTrace()
}
return null
}
2.Java
private File WriteSd(String str) {
System.out.println("字符串打印:" + str);
try {
String dirPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getPath() + "/contact";
File dir = new File(dirPath);
if (!dir.exists()) {
//创建目录
dir.mkdirs();
}
File file = new File(dir.getCanonicalPath() + "/" + "contact.json");
if (file.exists()) {
file.delete();
}
file.createNewFile();
FileOutputStream out = new FileOutputStream(file);
OutputStreamWriter outputStreamWrite = new OutputStreamWriter(out, "utf-8");
outputStreamWrite.write(str);
outputStreamWrite.close();
out.close();
System.out.println("文件写入成功:" + file.getCanonicalPath());
return file;
} catch (Exception e) {
System.out.println("文件写入失败:" + e.getMessage());
e.printStackTrace();
}
return null;
}
3.引用
1.kotlin
val file: File = WriteSd(json)!!
2.Java
File file = WriteSd(json);
总结
这篇文章比较长,懒得分开成几篇博客去写,直接一篇归纳得了,需要的认真看完,需要当中单独哪一个都可以,欢迎指正!