Bitmap与File与Uri之间的简单记载

作者: 难得糊涂与君勉 | 来源:发表于2019-02-28 10:52 被阅读9次

    简介:

    感觉Uri 、File、bitmap 比较混乱,这里进行记载,方便以后查看.

    1、将一个文件路径path转换成File

    String path ;
    File file = new File(path)
    

    2、讲一个Uri转换成一个path

    以选择一张图片为例:
    
    String path  = FileTools.getRealPathFromUri(content,uri);
    //自定义方法在下面
     public static String getRealPathFromUri(Context context, Uri uri) {
    
            if (null == uri) return null; //传入的Uri为空,结束方法
    
            final String scheme = uri.getScheme(); //得到Uri的scheme
    
            String realPath = null;
    
            if (scheme == null)
                realPath = uri.getPath();  //如果scheme为空 
            else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
                realPath = uri.getPath(); //如果得到的scheme以file开头
            } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
                //得到的scheme以content开头
                Cursor cursor = context.getContentResolver().query(uri,
                        new String[]{MediaStore.Images.ImageColumns.DATA},
                        null, null, null);
                if (null != cursor) {
                    if (cursor.moveToFirst()) {
                        int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                        if (index > -1) {
                            realPath = cursor.getString(index);
                        }
                    }
                    cursor.close(); //必须关闭
                }
            }
           
    //经过上面转换得到真实路径之后,判断一下这个路径,如果还是为空的话,说明有可能文件存在于外置sd卡上,不是内置sd卡.
            if (TextUtils.isEmpty(realPath)) {
                if (uri != null) {
                    
                    String uriString = uri.toString();
                    int index = uriString.lastIndexOf("/"); //匹配 / 在一个路径中最后出现位置
    
                    String imageName = uriString.substring(index);
                   //通过得到的最后一个位置,然后截取这个位置后面的字符串, 这样就可以得到文件名字了      
    
                    File storageDir;
    
                    storageDir = Environment.getExternalStoragePublicDirectory(
                            Environment.DIRECTORY_PICTURES); //查看外部储存卡公共照片的文件
    
                    File file = new File(storageDir, imageName);
                    //自己创建成文件,
    
                    if (file.exists()) {
                        realPath = file.getAbsolutePath();
                    } else {
    //                //那么存储在了外置sd卡的应用缓存file中
                        storageDir = context.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
                        File file1 = new File(storageDir, imageName);
                        realPath = file1.getAbsolutePath();
                    }
                }
            }
            return realPath;
    
    
      比如我在android 8.0 上运行的时候
      选择照片之后的Uri :  content://media/external/images/media/568344
      进行上面方法转换完之后: /storage/emulated/0/com.appben.appche/browser-photos/1550297407488.jpg
      
    
        }
    

    3、File 转换成path

    String path = file.getPath();
    将此抽象路径名转换为一个路径名字符串。所得到的字符串使用默认名称分隔符来分隔名称序列中的名称。 
    
    String path = file.getAbsolutePath();
    如果此抽象路径名已经是绝对路径名,则返回该路径名字符串,这与 getPath() 方法一样。如果此抽象路径名是空的抽象路径名,则返回当前用户目录的路径名字符串,
    该目录由系统属性 user.dir 指定。否则,使用与系统有关的方式分析此路径名。
    在 UNIX 系统上,通过根据当前用户目录分析某一相对路径名,可使该路径名成为绝对路径名。在 Microsoft Windows 系统上,
    通过由路径名指定的当前驱动器目录(如果有)来分析某一相对路径名,
    可使该路径名成为绝对路径名;否则,可以根据当前用户目录来分析它。 
    
    getCanonicalPath
    规范路径名是绝对路径名,并且是惟一的。规范路径名的准确定义与系统有关。如有必要,此方法首先将路径名转换成绝对路径名,
    这与调用 getAbsolutePath() 方法的效果一样,然后用与系统相关的方式将它映射到其惟一路径名。
    这通常涉及到从路径名中移除多余的名称(比如 "." 和 "..")、分析符号连接(对于 UNIX 平台),以及
    将驱动器名转换成标准大小写形式(对于 Microsoft Windows 平台)。 
    表示现有文件或目录的每个路径名都有一个惟一的规范形式。表示非存在文件或目录的每个路径名也有一个惟一的规范形式
    。非存在文件或目录路径名的规范形式可能不同于创建文件或目录之后同一路径名的规范形式。
    同样,现有文件或目录路径名的规范形式可能不同于删除文件或目录之后同一路径名的规范形式。
    
    

    下面是参看文章中提到的一个例子

    https://blog.csdn.net/qq_39949109/article/details/80609472
    
    File file = new File(".\\test1.txt");
    File file = new File("D:\\workspace\\test\\test1.txt");
      System.out.println("-----默认相对路径:取得路径不同------");
      System.out.println(file1.getPath());
      System.out.println(file1.getAbsolutePath());
      System.out.println("-----默认绝对路径:取得路径相同------");
      System.out.println(file2.getPath());
      System.out.println(file2.getAbsolutePath());
    
    
    结果是:
    -----默认相对路径:取得路径不同------
    .\test1.txt
    D:\workspace\test\.\test1.txt
    -----默认绝对路径:取得路径相同------
    D:\workspace\test\test1.txt
    D:\workspace\test\test1.txt
    
    
            File file = new File("..\\src\\test1.txt");
            System.out.println(file.getAbsolutePath());
            System.out.println(file.getCanonicalPath());
    //得到的结果
    D:\workspace\test\..\src\test1.txt
    D:\workspace\src\test1.txt
    

    4、URI 与Uri的区别
    URI 是java.net的子类
    Uri 是android.net的子类,Uri不能被实例化

    5、URI 转换成 File

    File file = null;
    try{
         file = new File(new URI(uri.toString()));
    }catch(URISyntaxException e){
        e.printStackTrace();
    }
    

    6、File 转换成URI

    URI uri = file.toURI();
    

    7、Path 转换成Uri

    Uri uri = Uri.parse(path);
    

    8、图片的Uri转Bitmap

    Bitmap bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(uri))
    

    9、File 转到bitmap

    Bitmap bitmap = BitmapFactory.decodeFile(file.getPath); //这个file要是真实路径创建的file
    

    10、bitmap 转 file,可以理解为将bitmap进行保存.

    //自己创建想要保存的文件的文件对象
    BuffferedOutPutStream bos = 
    new BufferedOutputStream(new FileOutputStream(file));
    bos.flush;
    bos.close;
    
    

    相关文章

      网友评论

        本文标题:Bitmap与File与Uri之间的简单记载

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