androidgetpath
Ⅰ android怎样拿到file的路径
Environment 常用方法:
* 方法:getDataDirectory()
解释:返回 File ,获取 Android
数据目录。
* 方法:getDownloadCacheDirectory()
解释:返回 File ,获取 Android
下载/缓存内容目录。
* 方法:getExternalStorageDirectory()
解释:返回 File ,获取外部存储目录即
SDCard
* 方法:(String type)
解释:返回 File
,取一个高端的公用的外部存储器目录来摆放某些类型的文件
* 方法:getExternalStorageState()
解释:返回 File
,获取外部存储设备的当前状态
* 方法:getRootDirectory()
解释:返回 File ,获取 Android 的根目录
file的getPath getAbsolutePath和getCanonicalPath的不同
File的这三个方法在api中都有说明,仅以程序为例说明。
package test;
import
java.io.File;
import java.io.IOException;
public class TestFilePath
{
public static void main(String[] args) {
// TODO Auto-generated
methodstub
System.out.println(System.getProperty("user.dir"));
try
{
System.out.println("-----默认相对路径:取得路径不同------");
File file1 =new
File("..\\src\\test1.txt");
System.out.println(file1.getPath());
System.out.println(file1.getAbsolutePath());
System.out.println(file1.getCanonicalPath());
System.out.println("-----默认相对路径:取得路径不同------");
File
file =new
File(".\\test1.txt");
System.out.println(file.getPath());
System.out.println(file.getAbsolutePath());
System.out.println(file.getCanonicalPath());
System.out.println("-----默认绝对路径:取得路径相同------");
File
file2 =new
File("D:\\workspace\\test\\test1.txt");
System.out.println(file2.getPath());
System.out.println(file2.getAbsolutePath());
System.out.println(file2.getCanonicalPath());
}
catch (IOException e) {
// TODOAuto-generated catch
block
e.printStackTrace();
}
}
}
程序执行结果如下:
F:\eclipseworkspace\testejb
-----默认相对路径:取得路径不同------
..\src\test1.txt
F:\eclipseworkspace\testejb\..\src\test1.txt
F:\eclipseworkspace\src\test1.txt
-----默认相对路径:取得路径不同------
.\test1.txt
F:\eclipseworkspace\testejb\.\test1.txt
F:\eclipseworkspace\testejb\test1.txt
-----默认绝对路径:取得路径相同------
D:\workspace\test\test1.txt
D:\workspace\test\test1.txt
D:\workspace\test\test1.txt
结论:
当输入为绝对路径时,返回的都是绝对路径。
当输入为相对路径时:
getPath()返回的是File构造方法里的路径,是什么就是什么,不增不减
getAbsolutePath()返回的其实是user.dir+getPath()的内容,从上面F:\eclipseworkspace\testejb、F:\eclipseworkspace\testejb\..\src\test1.txt、F:\eclipseworkspace\testejb\.\test1.txt可以得出。
getCanonicalPath()返回的就是标准的将符号完全解析的路径
public String
getAbsolutePath()返回抽象路径名的绝对路径名字符串。
如果此抽象路径名已经是绝对路径名,则返回该路径名字符串,这与 getPath()
方法一样。如果此抽象路径名是空的抽象路径名,则返回当前用户目录的路径名字符串,该目录由系统属性 user.dir
指定。否则,使用与系统有关的方式分析此路径名。在 UNIX 系统上,通过根据当前用户目录分析某一相对路径名,可使该路径名成为绝对路径名。在 Microsoft
Windows
系统上,通过由路径名指定的当前驱动器目录(如果有)来分析某一相对路径名,可使该路径名成为绝对路径名;否则,可以根据当前用户目录来分析它。
返回:
绝对路径名字符串,它与此抽象路径名表示相同的文件或目录的
抛出:
SecurityException
- 如果无法访问所需的系统属性值。
另请参见:
isAbsolute()
public String getCanonicalPath()
throws
IOException返回抽象路径名的规范路径名字符串。
规范路径名是绝对路径名,并且是惟一的。规范路径名的准确定义与系统有关。如有必要,此方法首先将路径名转换成绝对路径名,这与调用
getAbsolutePath() 方法的效果一样,然后用与系统相关的方式将它映射到其惟一路径名。这通常涉及到从路径名中移除多余的名称(比如 "." 和
"..")、分析符号连接(对于 UNIX 平台),以及将驱动器名转换成标准大小写形式(对于 Microsoft Windows
平台)。
表示现有文件或目录的每个路径名都有一个惟一的规范形式。表示非存在文件或目录的每个路径名也有一个惟一的规范形式。非存在文件或目录路径名的规范形式可能不同于创建文件或目录之后同一路径名的规范形式。同样,现有文件或目录路径名的规范形式可能不同于删除文件或目录之后同一路径名的规范形式。
返回:
表示与此抽象路径名相同的文件或目录的规范路径名字符串
抛出:
IOException
- 如果发生 I/O 错误(可能是因为构造规范路径名需要进行文件系统查询)
SecurityException -
如果无法访问所需的系统属性值,或者存在安全管理器,且其 SecurityManager.checkRead(java.io.FileDescriptor)
方法拒绝对该文件进行读取访问
从以下版本开始:
JDK1.1
Ⅱ Android开发中怎么将照片存储到系统相册中
//创建输出流,指向SD卡的Pictures文件夹
finalFiledirectory=Environment.(Environment.DIRECTORY_DCIM);
//创建保存的文件
Filefile=newFile(directory,"test.png");
FileOutputStreamout=null;
try{
out=newFileOutputStream(file);
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
}
//bitmap就是你的图片
bitmap.compress(Bitmap.CompressFormat.PNG,100,out);
Ⅲ android系统中如何判断一个文件是否存在
sd卡目录请不要直接使用/storage/sdcard0/,你这样试试x0dx0aString SDPATH = Environment.getExternalStorageDirectory().getPath() + "//"; x0dx0aFile f = new File(SDPATH +"/Manual/test.pdf")x0dx0a另外你是不是忘记加权限了
Ⅳ Environment.getExternalStorageDirectory().getPath()得到的是什么
以前的Android(4.1之前的版本)中,SDcard跟路径通过“/sdcard”或者“/mnt/sdcard”来表示存储卡,而在Jelly
Bean系统中修改为了“/storage/sdcard0”,以后可能还会有多个SDcard的情况。
目前为了保持和之前代码的兼容,sdcard路径做了link映射。
为了使您的代码更加健壮并且能够兼容以后的Android版本和新的设备,请通过Environment.getExternalStorageDirectory().getPath()来获取sdcard路径,
Ⅳ android中uri怎么转换成文件路径
方法1、用URLDecode解码就可以了。
String code = "D:/%e5%ad%a6%e6%b5%b7/My%20Course/%e8%ae%a1%e7%ae%97%e4%b8%8e%e8%bd%af%e4%bb%b6%e5%b7%a5%e7%a8%8b/Java%20workspace/my Project/bin/";
try{
String src=URLDecoder.decode(code,"UTF-8");//注意编码和输入时一致
System.out.print(src);
}catch (UnsupportedEncodingException e){
e.printStackTrace();
}
======
显示:
D:/学海/My Course/计算与软件工程/Java workspace/my Project/bin/
方法2:URI有自己的getPath方法,直接返回的就是解码后的路径
比如System.out.print(url.getPath());
Ⅵ android 我想读取一个txt文件,怎么获取路径
你的这个文件是不是存在手机的内存卡里面呢,如果是可以这样做
//首先获取到手机内存卡的根路径
String rootPath = Environment.getExternalStorageDirectory().getPath();
File file = new File(rootPath + "/a.txt"); //假设文件就在内存卡的根目录下
得到file对象之后就跟Java一样处理了
Ⅶ 求助一个Android 文件选择器的源码(用于上传文件时选择并...
打开文件选择器:
private void showFileChooser() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult( Intent.createChooser(intent, "Select a File to Upload"), FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
}
}
选择结果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case FILE_SELECT_CODE:
if (resultCode == RESULT_OK) {
// Get the Uri of the selected file
Uri uri = data.getData();
String path = FileUtils.getPath(this, uri);
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
FileUtils文件
public class FileUtils {
public static String getPath(Context context, Uri uri) {
if ("content".equalsIgnoreCase(uri.getScheme())) {
String[] projection = { "_data" };
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri, projection,null, null, null);
int column_index = cursor.getColumnIndexOrThrow("_data");
if (cursor.moveToFirst()) {
return cursor.getString(column_index);
}
} catch (Exception e) {
// Eat it
}
}
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
}