I am useing file chooser to choose a simple text file, however problem is that it returns path in
content:/com.android.externalrnalstorage.documents/document/1BF4-1E18%3Astudents.txt
(students.txt is located at sdcard).
I tried all getpath() functions available everywhere on net, but they dont work for me. They either give me /document/1BF4-1E18:students.txt
or the one with kitkat compatiblity gives me content:/com.android.externalstorage.documents/document/1BF4-1E18%3Astudents.txt.
I have really tried, so is there any way I can read file using content:/com.... without any conversions?
Thanks to CommonsWare, I got it working, Here is the snippet.
ContentResolver resolver = getContentResolver();
InputStream in = resolver.openInputStream(uri);
BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
I have really tried, so is there any way I can read file using content:/com.... without any conversions?
I do not know what "without any conversions" means here. To read in the content from a content:// Uri, use a ContentResolver and openInputStream().
Related
Using Glide, the following uri path to a png file is loaded without problems. In Android, the following code results in "file not found exception". Why? What do I need to do with the uri to make it work "everywhere" in Android?
uri = content://com.android.providers.media.documents/document/image%3A11322, as given by the default file chooser, used programmatically.
FileInputStream in = null;
try {
in = new FileInputStream(new File(uri));
} catch (FileNotFoundException e) {
Using BitMapFactory.decodeFile(uri) is even worse, since it also gives the same exception because it cannot find the file on the path = content:/com.android…. Important: Note that BitmapFactory removed one of the two slashes, for whatever reason. A bug?
Thanks for any help that can convert this work down from a two day "seek and find" the solution, down to the expected two minute job. Android contains an amazing number of built-in traps, I must say.
You cannot open a FileInputStream on a content scheme uri.
Open an InputStream on the uri instead.
InputStream is = getContentResolver().openInputStream(uri);
You can use the stream as before.
BitmapFactory.decodeStream(is);
When trying to get the ExifInterface I keep seeing a Raw image not detected error message.
ExifInterface exifInterface = new ExifInterface(filepath);
int rotation=exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION,ExifInterface.ORIENTATION_UNDEFINED);
Does anyone know what could be causing this?
I am getting it from a Uri but I know the filepath exists
Those statements are mutually contradictory. A Uri is not a file. If the scheme of the Uri is file, then and only then can you get a filesystem path to the file, by means of getPath(). If the scheme is anything else, such as content, then you cannot get a filesystem path, because there is no requirement that there be a file. For example, a Uri of http://stackoverflow.com/questions/42930509/exifinterface-jni-raw-image-not-detected-error does not mean that the Android device has a file at /questions/42930509/exifinterface-jni-raw-image-not-detected-error.
The ExifInterface from com.android.support:exifinterface (e.g., where the current latest version is 25.3.0) has a constructor that takes an InputStream. Create a ContentResolver (via getContentResolver() on a Context, such as your Activity). Call openInputStream() on that ContentResolver, supplying the Uri (works for both file and content schemes). Pass that InputStream to the library's ExifInterface constructor. This simultaneously ensures that you do not cause security problems for your users and avoids having to worry about getting a filesystem path for the content that you wish to examine.
Long story short, apply the below example to your code:
Uri uri = Uri.fromFile(f);
// where f of type File
in = context.getApplicationContext().getContentResolver().openInputStream(uri);
// context should refer to your context app
ExifInterface exifInterface = new ExifInterface(in);
// you'll need "exifInterface"
And don't forget to close your input stream
in.close();
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.
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();
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);
In android, I get a ParcelFileDescriptor after I call mContext.getContentResolver().openFileDescriptor(contentURI, "r")
but how can I read the content of the file into a string or Stringbuffer?
Thank you.
I would recommend that you use openInputStream() on ContentResolver instead. Then, you just use normal Java I/O to read it in.
I am trying to get a FileInputStream object on an image that the user selects from the picture gallery. This is the android URI returned by android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI
content://media/external/images/media/3
When I try to construct a java URI object from this object, I get an IllegalArgumentException with the exception description Expected file scheme in URI: content://media/external/images/media/3 whereas the android URI shows the scheme as content
Update:
Never found a solution for the original question. But if you want the byte stream of an image in the pictures gallery, this piece of code will do that.
Bitmap bitmap = Media.getBitmap(getContentResolver(), imageUri);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
ByteArrayInputStream fileInputStream = new ByteArrayInputStream(bytes.toByteArray());
You could use the toString method of the android Uri in combination of the String based constructor of the Java URI.
android.net.Uri auri = new android.net.Uri(what ever);
java.net.URI juri = new java.net.URI(auri.toString());
Android URI |
Java URI
Found the correct way to open InputStream from content URI:
InputStream fileInputStream=yourContext.getContentResolver().openInputStream(uri);
That's all!
There is a solution to your original question (convert Uri to URI):
Get the real file path (look this code: Get filename and path from URI from mediastore)
Get the URI using the real path and the constructor: URI(String uri)
If you need more details, look here:
How to delete a video recorded using an Intent with ACTION_VIDEO_CAPTURE?
I voted for jgilrincon's answer. I can't comment due to low reputation, and here goes some additional info - you can use FileHelper.java from Apache Cordova project, it has functions that you need for file handling from Uri strings, considering mediastore as well (and app assets folder)
Particularly this method provides InputStream from Uri:
public static InputStream getInputStreamFromUriString(String uriString, Activity cordova)
Since the String constructing doesn't work have you tried just constructing it your self?
android.net.URI auri = new android.net.URI(what ever);
java.net.URI juri = new java.net.URI(auri.getSchema(),
auri.getSchemaSpecificPart(),
auri.getFragment());
You might also want to double check that your getting valid data out of Android URI class. The docs as listed in my other answer discuss how it does pretty much no error checking. If there is infact an error the class just spits out garbage anyway and doesn't throw any exceptions. Which could very likely be why the java class which does do validation is throwing an exception.