当前位置: 移动技术网 > IT编程>移动开发>Android > Android中Bitmap、File与Uri之间的简单记录

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

2019年07月29日  | 移动技术网IT编程  | 我要评论

香港拳霸,沙塔纳城的伊恩,2366网页游戏

简介:

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

bitmap、file与uri

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;

总结

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

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网