美文网首页
android 文件读取、图片,视频刷新相册等

android 文件读取、图片,视频刷新相册等

作者: 谦谦行者 | 来源:发表于2023-04-13 09:57 被阅读0次

    设置targetSDK = 29 ,大于29需要适配分区存储,沙盒机制等
    请求获取 READ_EXTERNAL_STORAGE、WRITE_EXTERNAL_STORAGE
    获取文件读写权限

    bitmap 存储指定目录,刷新相册图库

    fun saveImageToGallery(context: Context, bmp: Bitmap): String {
            //生成路径
            val root: String = Environment.getExternalStorageDirectory().getAbsolutePath()
            val dirName = "名字随便取"
            val appDir = File(root, dirName)
            if (!appDir.exists()) {
                appDir.mkdirs()
            }
    
            //文件名为时间
            val timeStamp = System.currentTimeMillis()
           // 注意文件名称里不能有:冒号,否则创建文件失败,提示没有权限,但是权限明明获取了,这里说的没有权限是值linux系统里,
          // 文件名称不能带有: ,不是android系统的读写文件权限没有获取,总之不要整花里胡哨的名字,不要带特殊字符
            val sdf = SimpleDateFormat("yyyy-MM-dd_HH-mm-ss")
            val sd: String = sdf.format(Date(timeStamp))
            val fileName = "$sd.jpg"
    
            //获取文件
            val file = File(appDir, fileName)
            Log.e("yxk", file.absolutePath)
            var fos: FileOutputStream? = null
            try {
                fos = FileOutputStream(file)
                bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos)
                fos.flush()
                //通知系统相册刷新
                context.sendBroadcast(
                    Intent(
                        Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                        Uri.fromFile(File(file.getPath()))
                    )
                )
    //            return 2
                return file.path
            } catch (e: FileNotFoundException) {
                e.printStackTrace()
            } catch (e: IOException) {
                e.printStackTrace()
            } finally {
                try {
                    fos?.close()
                } catch (e: IOException) {
                    e.printStackTrace()
                }
            }
    //        return -1
            return ""
        }
    

    存入视频后刷新本地媒体库
    https://www.jianshu.com/p/6522269c4fdb

    // 扫描本地mp4文件并添加到本地视频库
    
                            val mMediaScanner = MediaScannerConnection(this@PlayerCameraActivity, null)
                            mMediaScanner.connect()
                            if (mMediaScanner != null && mMediaScanner.isConnected) {
    // FileUtils.listFilesInDir 是utilcodex 第三方工具类,作用是获取指定目录下所有文件
                                mMediaScanner.scanFile(FileUtils.listFilesInDir(appDir)[0].path, "video/mp4")
                            }
    

    调用本地播放器播放视频文件
    https://blog.csdn.net/Ryfall/article/details/128142720

    1.新建一个类并继承FileProvider类,不做其他事情

    public class AppFileProvider extends FileProvider {
    }
    

    2.AndroidManifest.xml中配置

    
    <provider
                android:name=".ui.file.view.AppFileProvider"
                android:authorities="你的包名.fileprovider"
                android:exported="false"
                android:grantUriPermissions="true">
                <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/file_paths" />
            </provider>
    

    3.res文件夹下新增xml文件夹,然后添加file_paths.xml文件,内容如下:

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        tools:ignore="MissingDefaultResource">
        <files-path
            name="int_root"
            path="/" />
        <external-path
            name="ext_root"
            path="/"/>
        <external-files-path
            name="ext_file"
            path="/"/>
        />
        <external-cache-path
            name="ext_path"
            path="/"/>
     
        />
    </paths>
    

    4.调用系统播放器播放视频代码

    private void playVideo(String filePath, Context context){
            Intent intent = new Intent(Intent.ACTION_VIEW);
            File file = new File(filePath);
            Uri uri=null;
            if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){
                uri = FileProvider.getUriForFile(context,"你的包名.fileprovider", file);
            }else {
                uri = Uri.fromFile(file);
            }
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(uri, "video/*");
            context.startActivity(intent);
        }
    

    5.调用系统图库展示图片代码

    private void playImage(String filePath, Context context){
            Intent intent = new Intent(Intent.ACTION_VIEW);
            File file = new File(filePath);
            Uri uri=null;
            if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){
                uri = FileProvider.getUriForFile(context,"你的包名.fileprovider", file);
            }else {
                uri = Uri.fromFile(file);
            }
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.setDataAndType(uri, "image/*");
            context.startActivity(intent);
        }
    

    相关文章

      网友评论

          本文标题:android 文件读取、图片,视频刷新相册等

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