Getting String Based Image Location from Drawable Directory - android

I am trying to use PDFjet library to print a page to PDF in Android.
The documentation tells me that in order to print an image to PDF, I need to get a handle on the image filename like:
String fileName = "images/myimage.png";
So now i want to print an image from the res/drawable folder. In Android the resources are referenced through R.id which returns an int.
My question is:
How do i get a handle on the image from the drawable folder as string ?

try this:
String fileName = "android.resource://your.package.name/" + R.drawable.xxx;;

Related

Get fileName from uri on KitKat Document

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);

Cannot Open Image in assets folder for Android App

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!

using files stored in the SDCard as resourses for internal website on a Browser

I need to download some resources from the net, then locally process them and show them on a BrowserActivity.
Since the resources (images) could be really big I'm saving them in the SDCard.
How may I refer to those stored resources from an html page? The html page is embedded into my app (It's in assets folder). I think this will fire some kind of security issues.
Is this the right approach? or is there any better solution?.
Thanks
Answer for question 1): You can refer to file on your sd card using "file://" prefix. See example below:
String fileName = "x"; // here is the file name of the downloaded image
String imagePath = "file://" + getApplicationContext().getFileStreamPath(fileName.getAbsolutePath();
String html = html+"<img src=\"" + imagePath + "\"/>" // embed that code in your modified html source.

Cannot open image file stored in sdcard

In my application I save the contents of a tableLayout as an image in a folder. To allow user to open a file from the saved images I've created a text file that contains the names of these files. These file names will be loaded in an array (files) later. User clicks on "open" to see a list of file names and selects the one he wants to open. I'm using the following code to open a file.
final String imageInSD = extStorageDirectory+"/myFolder/"+files[which];
//where 'files' is an array of strings and contains the names of files.
//and 'which' is the index of the selected element in the list
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
ImageView ivv=(ImageView) findViewById(R.id.imageView);
ivv.setImageBitmap(bitmap);
when I try it nothing happens so I tried the following
final String imageInSD = extStorageDirectory+"/myFolder/myFile.PNG";
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
ImageView ivv=(ImageView) findViewById(R.id.imageView);
ivv.setImageBitmap(bitmap);
And it shows the image named myFile.
I've already checked if I'm getting the correct file name and path and it seems to be correct. (when i click on myFile.PNG in the list and show the path I get "/mnt/sdcard/myFolder/myFile.PNG").
Why doesn't it work when I use the first code?
String concatenation isn't a good way to combine paths. It is better to use the File constructor :
File directory = new File(extStorageDirectory, "myFolder");
File fileInDirectory = new File(directory, files[which]);
Bitmap bitmap = BitmapFactory.decodeFile(fileInDirectory.getAbsolutePath());

How can I load an image from drawable folder?

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

Categories

Resources