2012-10-31 61 views
0

我正在使用下面的代码来浏览和从SD卡中选择文件。在Android中打开文件

Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
intent.setType("file/*"); 
startActivityForResult(intent,PICKFILE_RESULT_CODE); 

当用户选择了一个文件,我取使用此行代码的路径

String picturePath = data.getData().getPath(); 

但是当我尝试使用打开该文件下面的代码,我收到文件未发现异常

InputStream inputStream = getContentResolver().openInputStream(Uri.fromFile(new File(picturePath))); 

我得到这个作为路径选择的文件

/external/images/media/36 

我该如何在android中做到这一点?

感谢

+0

你用什么版本的android测试? – Marcelo

+0

我正在使用android 2.2 – Zach

+0

该路径有些奇怪,它缺少文件扩展名。它看起来像你正在选择一个图像,所以应该有一个文件扩展名。 – Marcelo

回答

1

你尝试:

InputStream inputStream = getContentResolver().openInputStream(data.getData()); 

另外,如果你想使用一个文件:

http://developer.android.com/reference/java/io/FileInputStream.html

一定要使用getAbsolutePath(),而不是getPath();

+0

嗨对不起:)。它适用于某些设备,但在旧版本中它仍然返回与上述相同的内容。我正在测试android 2.2 – Zach