I am trying to get the Uri of an image I have in the drawable folder. I tried many possible ways but nothing seems to work. Can anyone suggest me how to get the Uri of res folder. Any help is much appreciated.
Well, it's actually quite easy. a base URI for a resource in your package would be something like the following possibilities:
android.resource://[package]/[resource_id]
android.resource://[package]/[res type]/[res name]
So the following would be managable.
String pkgName = getApplicationContext().getPackageName();
Uri path = Uri.parse("android.resource://"+pkgName+"/" + R.drawable.icon);
Uri otherPath = Uri.parse("android.resource://"+pkgName+"/drawable/icon");
You can find more about this at http://androidbook.blogspot.com/2009/08/referring-to-android-resources-using.html
Related
Since android 6.0, the sdcard path is no longer "/storage/sdcard1/", "/storage/sdcard-ext/" or something.
The path depends on the phone instead. If I use Nexus 5x AVD, the path is "/storage/1D15-3A1B/". When I use Nexus 6p AVD, the path is "/storage/4679-1802/". So how can I dynamically write the sdcard path in program to locate the file in external sdcard?
Thank you!
Have a look at getExternalFilesDirs().
It seems that I found the solution.
I'm using the access storage framework. I get it by handling the string of uri and get the "1D15-3A1B" part and then combine it to the sdcard path. Here is the solution.
When I click the file Welcome to Word.docx in the sdcard root path, I get the intent by onActivityResult().
Get the DocumentId by String docId = DocumentsContract.getDocumentId(intent.getData()), thus the string docId is "1D15-3A1B:Welcome to Word.docx".
Split the string and get a string array by String[] split = docId.split(":"), so we can get "1D15-3A1B" which is split[0], and the path in sdcard "Welcome to Word.docx" is split[1].
If we want to get the uri of the file we clicked before, just need to build a new string String newPath = "/storage/" + split[0] + "/" + split[1].
update:
Actually getExternalFilesDirs() is much easier to do this...Thank you #greenapps!
I was trying many approaches, including:
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
Environment.getExternalStorageDirectory()
Still my picture files in DCIM/Camera were not visible to my app. I could see the directory names, but listFiles() on any of these directories returned null.
Finally, and sheepishly, I found that my Manifest did not contain the magic line android.permission.WRITE_EXTERNAL_STORAGE.
Once I added the permission, it worked like a charm.
Moral of the story: Android does not warn/break/throw exception under this condition. When it fails, it fails silently!
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().
I want to get fileName from a uri... I can get a fullpath from Other apps like gallery or other third party file managers, but I can't get fileName from KitKat Document, I don't need a fullPath just the fileName with an inputStream is good for me...
Thanks
Edit: I found a piece of code that solves the issue almost, but it can't get filePath of none known types like *.exe *.vcf , ...
Here's the source
If you have the absolute path of file then simpley create the object of file and use getName method
File f = new File(<Full Path of your file>);
This will return file name with extension.
ANd if you do not want extension
then use CommonsI/O libarary
And from that library you can use
String fileNameWithOutExt = FilenameUtils.removeExtension(fileNameWithExt);
Just like the title says, i can't seem to get the images in my assets folder to open up.
public static final String CONTENT_URI = "file:///android:asset/";
Uri tmp = Uri.parse(CONTENT_URI + s);
holder.image.setImageURI(tmp);
I have tried a bunch of different syntax's, and I can't seem to get the asset to be detected. Keep getting resolveUri errors about it not finding the files.
s is a string such as hears.png which is indeed in the assets folder.
This is what you need to get images from asset
yourImageView.setImageBitmap(BitmapFactory.decodeStream(activity.getResources().getAssets().open("whateverResorce.png")));
Regards!
I've the file name of an image stored in the drawable? how can I open it? I want to know the path?
the file name is String imageFileName = "image.png"
and I want to have the path string something like file://drawable/image.png
You can open it using openRawResource(). See more at the documentation.
See this post regarding how to construct a uri to raw resource, though as far as I know it is not recommended.
You can access it through its resource ID. See previous answers such as this one:
Select Drawable file path
Here is the code segment showing how you can refer to an image through its resource ID.
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.pic1);
int pic_width = mBitmap.width();
int pic_height = mBitmap.height();
See here for more detail