服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android中Bitmap、File与Uri之间的简单记录

Android中Bitmap、File与Uri之间的简单记录

2022-09-27 16:00难得糊涂与君勉 Android

这篇文章主要给大家介绍了关于Android中Bitmap、File与Uri之间的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧

简介:

感觉Uri 、File、bitmap 比较混乱,这里进行记载,方便以后查看.下面话不多说了,来一起看看详细的介绍吧

Bitmap、File与Uri

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

?
1
2
String path ;
File file = new File(path)

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

以选择一张图片为例:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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 平台)。
表示现有文件或目录的每个路径名都有一个惟一的规范形式。表示非存在文件或目录的每个路径名也有一个惟一的规范形式
。非存在文件或目录路径名的规范形式可能不同于创建文件或目录之后同一路径名的规范形式。
同样,现有文件或目录路径名的规范形式可能不同于删除文件或目录之后同一路径名的规范形式。

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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

?
1
2
3
4
5
6
File file = null;
try{
 file = new File(new URI(uri.toString()));
}catch(URISyntaxException e){
 e.printStackTrace();
}

6、File 转换成URI

?
1
URI uri = file.toURI();

7、Path 转换成Uri

?
1
Uri uri = Uri.parse(path);

8、图片的Uri转Bitmap

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

9、File 转到bitmap

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

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

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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对服务器之家的支持。

原文链接:https://www.jianshu.com/p/b3cc6804a9a5

延伸 · 阅读

精彩推荐