Android从Uri获取真实路径和文件名 [英] Android get the real path and file name from Uri
问题描述
public void fileSearching(){
try{
// find1, find2 are buttons that open the file explorer
find1.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
Intent i = new Intent();
i.setAction(Intent.ACTION_GET_CONTENT);
i.setType("file/*");
check = 0;
startActivityForResult(i, CODE);
}
});
find2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent();
i.setAction(Intent.ACTION_GET_CONTENT);
i.setType("file/*");
check = 1;
startActivityForResult(i, CODE);
}
});
}
catch (Exception ex){}
}
@Override
public void onActivityResult(int request, int result, Intent data){
if (request == CODE && result == RESULT_OK){
Uri u = data.getData();
String path = data.getData().getPath();
}
}
重新运行 / external / file / 59 但我需要 / sdcard / downloads /... +文件名
我需要这个,因为我想得到一个sha-1,md5,... hash对于任何文件。
如果我使用/ external / file / xx,它会给我一个FileNotFoundException。
我尝试了一些已经提供的解决方案,但似乎没有为我工作或我不知道如何使用它$ / $>
Retrurns /external/file/59 but i need /sdcard/downloads/... + file name
I need this because i want to get a sha-1,md5,... hash for any file.
If i use the "/external/file/xx" it gives me a FileNotFoundException.
I've tried some solutions that were already presented but none seemed to work for me or i didn't know how to use it
推荐答案
Retrurns / external / file / 59
Retrurns /external/file/59
这是因为您没有看 Uri
。 getPath()
only 有意义,如果方案是 file
。您很少会使用文件
方案获得 Uri
。通常,它会有一个内容
方案,在这种情况下,路径对你来说毫无意义。
That is because you did not look at the scheme of the Uri
. getPath()
only has meaning if the scheme is file
. You will rarely get a Uri
with a file
scheme. Usually, it will have a content
scheme, in which case the path is meaningless to you.
我需要这个,因为我想为任何文件获得sha-1,md5,... hash。
I need this because i want to get a sha-1,md5,... hash for any file.
使用 ContentResolver
和 openInputStream()
来获取 InputStream
由 Uri
标识的内容。将 InputStream
传递给散列算法。
Use ContentResolver
and openInputStream()
to get an InputStream
on the content identified by the Uri
. Pass that InputStream
to your hashing algorithm.
这篇关于Android从Uri获取真实路径和文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!