user1130354
user1130354

Reputation: 21

convert Uri to file path but can not open it in nexus 4

I open a picture in album and get the Uri. Then I convert the Uri to a file path. In the log it shows as something like mnt/storage/emulated/0/xxx.jpg. I covert Uri to file path as the way like:

Cursor cursor = GlobalObjectManager.getInstance().getContext().getContentResolver()
                    .query(filePathUri, null, null, null, null);

int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
fileName = cursor.getString(column_index); 

The problem is that when I open the file with function it catches a FileNotFoundException.

String path = "mnt/storage/emulated/0/xxx.jpg";
FileInputStream in = new FileInputStream(path);

the code works well on other devices with Android 2.3-4.1. So far as I know is that my Nexus 4 runs Android 4.2 and mnt/storage/emulated/0/ works for multi-user.

In my app I must use FileInputStream() function to read byte data of the beginning of the file.

Could anyone tell me how to fix the bug? Thanks!

ok i fix it. I made a big mistake! I add mnt/ in front of storage/ needlessly, and it takes the bug.

Upvotes: 0

Views: 825

Answers (2)

ownwell
ownwell

Reputation: 1

I think you should not have the "mnt" ,this is to say,you can code as this:

String path = "mnt/storage/emulated/0/xxx.jpg";

FileInputStream in = new FileInputStream(path);

Upvotes: 0

Hans-Christoph Steiner
Hans-Christoph Steiner

Reputation: 2682

I believe you are seeing /storage/emulated/0. We're seeing this problem too, it seems to be related to the new /storage handling for multiple SD cards, I think it was introduced in Android 4.1 but maybe later. If you look, you'll see that /storage/emulated/0 does not exist on the filesystem, not even as a symlink. Who knows what the system is using that path or what tricks are going on there.

The workaround is to do:

fileName = new File(cursor.getString(column_index)).getCanonicalPath();

Upvotes: 0

Related Questions