Android capture text messages, all application information, address book, call history, mobile phone system album pictures


foreword

Generally, when we make financial apps, we will encounter risk control needs. We need to obtain the user’s mobile phone text messages, all installed application information on the mobile phone, address book, call records and other functions. Next, let’s see how to do it. One article solves everything!


1. The permissions needed to grab these things

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));
                }
            }

2. Get text messages

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);

3. Obtain all application information of the mobile phone

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);

3. Get the address book

1. Obtain the address book tool class

/**
 * @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

Here I only need these fields, I need to add more

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 reference

//实例化工具类
 ContactUtils mobiletype = ContactUtils(this)
//list为MutableList<MessageBean>  
  list = mobiletype.information
  json = Gson().toJson(list)

4. Java reference

//实例化工具类
ContactUtils mobiletype = new ContactUtils(this);
//list为List<MessageBean>
 list = mobiletype.getInformation();
 json = new Gson().toJson(list);

4. Call records

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 reference

 /**
     * 获取通话记录
     */
    @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 reference

 /**
     * 获取通话记录
     */
    @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);
    }

5. System album

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. Convert time to timestamp

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. Methods of intercepting specific characters before and after

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;
    }

6. Write the JSON string to a file

把获取到这些转为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. Quote

1.kotlin

val file: File = WriteSd(json)!!

2.Java

File file = WriteSd(json);

Summarize

This article is relatively long, and I am too lazy to divide it into several blogs to write. I can sum it up in one article. If you need to read it carefully, you can use any one of them. Welcome to correct me!

Guess you like

Origin blog.csdn.net/Android_Cll/article/details/131653890