美文网首页
Android Base64编码保存本地。查询文件夹大小以及删除

Android Base64编码保存本地。查询文件夹大小以及删除

作者: 爱言语论 | 来源:发表于2020-08-07 09:13 被阅读0次

    代码如下

    package com.example.file
    
    import android.content.Context
    import android.content.Intent
    import android.graphics.Bitmap
    import android.graphics.BitmapFactory
    import android.net.Uri
    import android.os.Bundle
    import android.os.Environment
    import android.util.Base64
    import android.widget.Toast
    import androidx.appcompat.app.AppCompatActivity
    import kotlinx.android.synthetic.main.activity_main.*
    import java.io.File
    import java.io.FileOutputStream
    import java.io.IOException
    import java.text.SimpleDateFormat
    import java.util.*
    
    class MainActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            save.setOnClickListener {
                savePicPhone(this, Base642Bitmap())
                getLength()
            }
            delete.setOnClickListener {
                delete()
            }
        }
    
        fun Base642Bitmap(
            str: String = "iVBORw0KGgoAAAANSUhEUgAAAEgAAABICAYAAABV7bNHAAAN0ElEQVR42u1caVBUVxpNMknVrFX5kZrMZPyRqqnJn6RUXFH2ZukGGhAF9y0kknGJCwKyNXSz76hJXDCJOnGJigpRwRhZlH1fRECgG9BUjElM4hJEn9aZ73tIxOY9E0EEKV7VqW7eeX3v/Q73ft+5r5fnnhs9Ro/RY/QYPZ7xA3jeOlf74kgCx/REtNFC+8JI/b8POLbeDdjoo50UhugIhT4mWdEak2ijj01yMiQlubdvTuLnUlAbUpLcDZtkeVfiZzyCZ46vkeO5bfWjeBobj5Gf85jFsVMMHMuARep54bxvtr+ibkvOVRuSwHDWP8Dcto/wTsfHD53rjfltW/F2+w5ZflHbNixp3y7LM8fXyPHcNvchx/PYeIzG57tjSc6ddyHxlf6J1Gt9uhiS8yxaImBxIVwwhnNLojCn/QNBimO4tCYLngZ53q01RZhp2CTLM8fXyPHcNvchx/PYeIySPMXkVB9zlkJ8wTjm3zzEJEaHbWOE2tWQ3C1OM4lkBOfW7lkkxTFc9SmYbfh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wGyvIIGZ9DApFTVlEplxAHZvUkUGMcTDNW9/+7GlYVoYP+Ic6ZLRspYH/JUu5ERlDFZVzG53DeUVGVkvY5IfCoT4aqLlpKHJizQE3xmHJszUBmUMjGp/GFOq5uHLRxtXKipKyiZSjnc9woIasqImV8zn2BqFL1EYe2QeYN3QJNTluR2m+ByIYvFmdQc7hgfiF8cD8GTCJx4L1LuRNVLFWxTtbnuFXFQVUZKeNzNPA4n9ItkJE4ZufpsUknOum3UuYs51gnbvd+6XG/1PsHwj/tzoZUieK0RAgsVG+oWxOFOR0f9jnfAxJX8OQv/crwbvoUYWb7pgfnLugE05JAYcpZf2Fqvr+gLg4XHIt1wtTCAGFqUYBgWhwo8qalQYJpWZBA5VxwqooUeOPJeyvePrBDZhPIPsejIUVwqo8ROCFzzjE/T6CZw+KYNWhh+cW6WopxjBjr4/7QgOdBTxbo5TH2JlaWWb61ytooODWTJ2lN/BVz9R/Cq23HQ+d6Y76eyrwhVZZfpN8Kfm/qofPUhwPtn+yKQuFZGg+P0jjYlYTRrlxLG09CuY54nbiFWFBDe70aqlTV5KtqIikhR1HOIZyLhiNhSdMWeDZuhGNDLJwaCU1xIpTVkbBIX1f3L8WbCo7RWmv94nP9OV63fv2P9PAaYfwbGpeg8TuX7p2w3/uwyZ53D5ns804z3bc8zeLA6jR+brJ3Wfdjz3OC6b6VaRb7Vj3gjK6ZTrwZ8xLc2NQlaZO3LiV4pY3duqgb2wmpi0Ru7PYlaaafeKdN/vidtHGfLL2PJd3Y6ZU2bpdXmtmu5WmT/0ft7XonzeQzGvOedw+P/XjpXo6FY+LYxkwb86eB/PjC869NfO3P9PgPwn8IbxLeesbx5v1YXn117Kt/4RgH/iMXEye+9PL411/+2xuvvfLXf7/692cZHAPHwjENxu+BPD9CMHqMHqPH6PFMHP8HwAE4ofkuCQcAAAAASUVORK5CYII="
        ): Bitmap {
            val byte = Base64.decode(str, Base64.DEFAULT)
            return BitmapFactory.decodeByteArray(byte, 0, byte.size)
        }
    
        @Synchronized
        fun savePicPhone(context: Context, bmp: Bitmap) {
            var fos: FileOutputStream? = null
            val path: String = Environment.getExternalStorageDirectory().getPath()
            try { // 获取系统时间
                val d = Date()
                val f = SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
                val strDate: String = f.format(d)
                val div = File(path, "/logo")
                if (!div.exists()) {
                    div.mkdirs() // 不存在就创建
                }
                val file = File(div, strDate + "_picture.jpg")
                fos = FileOutputStream(file)
                val compress: Boolean = bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos)
                fos.flush()
                // 发送更新图库广播
                if (null != file) {
                    val intent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
                    val uri: Uri = Uri.fromFile(file)
                    intent.setData(uri)
                    context.sendBroadcast(intent)
                }
            } catch (e: Exception) {
                e.printStackTrace()
            } finally {
                if (fos != null) {
                    try {
                        fos.close()
                    } catch (e: IOException) {
                        e.printStackTrace()
                    }
                    fos = null
                }
            }
        }
    
    
        fun getLength() {
            val path = Environment.getExternalStorageDirectory().path+"/picture"
            val file = File(path)
            var size: Long = 0L;
            if (file.exists()) {
                if (file.isDirectory) {
                    for (file1 in file.listFiles()) {
                      if(file1.name.contains("picture")){
                          size += file1.length()
                      }
                    }
                }
            }
            Toast.makeText(this, "$size", Toast.LENGTH_SHORT).show()
        }
    
        fun delete(){
            val path = Environment.getExternalStorageDirectory().path+"/picture"
            val file = File(path)
            if(file.exists()&&file.isDirectory){
              for(file1 in file.listFiles()){
                  file1.delete()
              }
            }
    
            getLength()
    
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Android Base64编码保存本地。查询文件夹大小以及删除

          本文链接:https://www.haomeiwen.com/subject/rtodrktx.html