Every file type associated with its specific icon.
Now if i am choosing somefilename.xxx then how i could be able to choose the icon associated with *.xxx rather than the default icon for unknown file.
Original Question
How can I get .txt
or .doc
or .png
file icon? For example now I am choosing filename.pdf
, how to get filename.pdf
icon?
For example
File f = new File("filename.pdf");
My File name is filename.pdf
. How to get this file icon (Adobereader Icon).
Try Using PackageManager.queryIntentActivities:
final Intent innt = new Intent(Intent.ACTION_VIEW);
innt.setData(fileUri);
innt.setType("application/pdf");
final List<ResolveInfo> matches = getPackageManager().queryIntentActivities(innt, 0);
for (ResolveInfo match : matches) {
final Drawable icon = match.loadIcon(getPackageManager());
final CharSequence label = match.loadLabel(getPackageManager());
}
First thing i would like to say any file do not contains it icon with them in general say if you don't have office installed you won't get doc icon or powerpoint icon.
so its system work to associate icon to specific file type so Go this way
To achieve your goal its highly recommended to include all icons that you are expected to use say if You choose some.png check for extention and associate the png.ico to this file icon
doing this way you will have to associate with every type of files.
Edit 1:
you can easily find out the icons on web add to it into your drawable folder's now for every file name extract the extension and set the icon according to the extension this way you can achieve your goal
Edit 2 :
To achieve your goal do the following :
add all icon files to your res\drawable folder for easyness set all
icon file name as its extension Say use doc.png for doc
extension for easyness
Now map all file extension to your icon file say check for file extension and then do something like following:
String ext = "Something";
switch(ext)
{
case "doc":
image.setImageResource(R.drawable.doc);
break;
case "exe":
image.setImageResource(R.drawable.exe);
break;
default :
image.setImageResource(R.drawable.default);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With