Is there any way that I can get the image path from drawable folder in android as String. I need this because I implement my own viewflow where I'm showing the images by using their path on sd card, but I want to show a default image when there is no image to show.
Any ideas how to do that?
These all are ways:
String imageUri = "drawable://" + R.drawable.image;
Other ways I tested
Uri path = Uri.parse("android.resource://com.segf4ult.test/" + R.drawable.icon);
Uri otherPath = Uri.parse("android.resource://com.segf4ult.test/drawable/icon");
String path = path.toString();
String path = otherPath .toString();
based on the some of above replies i improvised it a bit
create this method and call it by passing your resource
Reusable Method
public String getURLForResource (int resourceId) {
//use BuildConfig.APPLICATION_ID instead of R.class.getPackage().getName() if both are not same
return Uri.parse("android.resource://"+R.class.getPackage().getName()+"/" +resourceId).toString();
}
Sample call
getURLForResource(R.drawable.personIcon)
complete example of loading image
String imageUrl = getURLForResource(R.drawable.personIcon);
// Load image
Glide.with(patientProfileImageView.getContext())
.load(imageUrl)
.into(patientProfileImageView);
you can move the function getURLForResource to a Util file and make it static so it can be reused
If you are planning to get the image from its path, it's better to use Assets instead of trying to figure out the path of the drawable folder.
InputStream stream = getAssets().open("image.png");
Drawable d = Drawable.createFromStream(stream, null);
I think you cannot get it as String but you can get it as int by get resource id:
int resId = this.getResources().getIdentifier("imageNameHere", "drawable", this.getPackageName());
First check whether the file exists in SDCard. If the file doesnot exists in SDcard then you can set image using setImageResource() methodand passing default image from drawable folder
Sample Code
File imageFile = new File(absolutepathOfImage);//absolutepathOfImage is absolute path of image including its name
if(!imageFile.exists()){//file doesnot exist in SDCard
imageview.setImageResource(R.drawable.defaultImage);//set default image from drawable folder
}
Related
I want to add images to the ViewHolder. I do everything in method onBIndViewHolder(). I get String variable path from db, try to parse it by Uri and setImageUri, but it doesn't work. For debuggind I log path to the console.
String imgPath = images.get(position);
Log.d("imgTag", imgPath);
Uri imageURI = Uri.parse(imgPath);
//holder.image.setImageURI(imageURI);
Path of the image from log:
/document/image:46277
If I set image by setImageResource() it works fine, but I need from URI.
I have an assets subfolder and images on it all png's some bigger, I'm writing a hybrid app from Angular so I pass via an interface the name of a file to pop it in an activity I pass only the name not the extession. It works for some files but not for others (a bigger one on my case) is there a reason for that???, I'm using this basic code below, the smaller file resID has a number for the bigger file it comes out as 0. The png with the problem does load on a basic page in angular, I want to pop a zoomer of that image. Probably I could place a copy in the Drawable res folder, but Angular needs it hanging from my assets.
image = (TouchImageView) findViewById(R.id.cur_img);
if( getIntent().hasExtra("imageResName") ) // set it to passed image
{ // a popup pass in the image
String curResName =getIntent().getExtras().getString("imageResName");
// gte the di from a drawble ..it wprks
int resID = getResources().getIdentifier(curResName , "drawable", getPackageName());
image.setImageResource(resID);
}
UPDATE: Yep that code will NOT work I could pull an SO answer where
I probably misunderstood it ... this is the mod'ed code that will work
if( getIntent().hasExtra("imageResName") ) // set it to passed image
{ // a popup pass in the image
String curResName =getIntent().getExtras().getString("imageResName");
Bitmap bitmap=null;
try
{ // for image in: /assets/imagenes/xxx.png
AssetManager assetManager = getApplicationContext().getAssets();
InputStream istr = assetManager.open("imagenes/" + curResName + ".png" );
bitmap = BitmapFactory.decodeStream(istr);
istr.close();
}
catch (Exception ex)
{
bitmap = null;
}
finally
{
image.setImageBitmap(bitmap);
}
Hello guys i have the problem that i want to include an image in my project and when the user click the print button the image will be printet followed by other information but i dont get it to pass the image path to the PrintTool
PrintTool.printPhotoWithPath(imagePath, this);
and the first line in printtool are this
public static void printPhotoWithPath(String filePath, Context context) {
// Get the picture based on the path
File mfile = new File(filePath/*path*/);
if (mfile.exists()) {
Bitmap bmp = BitmapFactory.decodeFile(filePath/*path*/);
byte[] command = decodeBitmap(bmp);
printPhoto(command, context);
}else{
Log.e("PrintTools_58mm", "the file isn't exists");
}
}
So my problem is, how can i get the path from my image in drawable folder to the code?
Please remove this line
File mfile = new File(filePath/*path*/);
Provided the image is currently in your drawable directory, you can get it like this:
if (mfile.exists()) {
Bitmap bmp = BitmapFactory.decodeFile(getResources(), R.drawable.<yourDrawableName>);
byte[] command = decodeBitmap(bmp);
printPhoto(command, context);
}else{
Log.e("PrintTools_58mm", "the file isn't exists");
}
NB Replace with the name of your Drawable.
If you require the path of the Bitmap, by default it is
String imageUri = "drawable://" + R.drawable.image;
Refer to this.
i hope this helps you
File file = new File(String.valueOf(R.mipmap.imgeName));
file.getPath();
I am working with gif images at the moment and have followed the answer from the question here
From this I have the following line of code:
GifWebView view = new GifWebView(this, "http://cdn.lifebuzz.com/images/17487/lifebuzz-35df9a5ee351ae72711e5a293658e319-limit_2000.gif");
Now, I want to download this image, save it locally in the "drawable" folder or "assets" folder and then access it from there.
I have tried downloading the image into the "drawable" folder and trying :
GifWebView view = new GifWebView(this, "#drawable/myimg.gif");
I have also tried this:
int imageResource = R.drawable.myimg;
Drawable image = getResources().getDrawable(imageResource);
String imageUri = "drawable://" + R.drawable.myimg;
GifWebView view = new GifWebView(this, imageUri);
Still no luck.
I've tried accessing it from the assets folder but this seemed even more complicated.
I just would like to save it locally and access it locally, I don't really care where it is stored.
Thanks for any help.
EDIT
public GifWebView(Context context, String path) {
super(context);
loadUrl(path);
}
I have issue to retrieve image path from assets folder.
I have an image folder in assets folder. Within the folder i have three different folders.
Here is the code that i used:
String IntroImage1= "" + languageSelected + "/" + Intro1ImagePath + ".png" ;
try{
AssetManager mngr =getAssets();
InputStream BulletImgInput = mngr.open(IntroImage1);
//InputStream BulletImgInput = mngr.open("image/Malay/bullet.png");
Bitmap bitmapBullet = BitmapFactory.decodeStream(BulletImgInput);
BulletImage.setImageBitmap(bitmapBullet);
}catch(final IOException e){
e.printStackTrace();
}
I am wondering why can't i display the image? Because I have try to retrieve it through this code:
InputStream BulletImgInput = mngr.open("image/Malay/bullet.png");
It did retrieve the file, but with the string that i replaced in mngr.open it doesn't show up.
Really need you guys to help up.Thanks.
You don't need the AssetManager. You can do
BitmapFactory.decodeFile("file:///android_asset/image/Malay/bullet.jpg")
Though storing Images in the Assets is not the best way to go. You will not be able to take advantage of the Android resource management system. So unless you have a compelling reason to do this, I'd suggest that you take a look at using the res folder and the resources system.
Update: Explanation
The BitmapFactory has a method to decode a file via the decodeFile method. That's point one. Android lets you access a file in the assets folder via the file:///android_asset/{path} path. In your case, the Image at /image/Malay/bullet.jpg is the assets folder can be accessed via the the file:///android_asset/image/Malay/bullet.jpg.
Try this:
try {
// get input stream
InputStream ims = getAssets().open("avatar.jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
mImage.setImageDrawable(d);
}
catch(IOException ex) {
Log.e("I/O ERROR","Failed when ..."
}
your BulletImage
String url = "file:///android_asset/NewFile.txt";
String url = "file:///android_asset/logo.png";
you can access any file....
InputStream BulletImgInput = mngr.open("file:///android_asset/image/Malay/bullet.png");
Maybe this could work for u.
May be problem is in missed image part of path?
String IntroImage1= "image/" + languageSelected + "/" + Intro1ImagePath + ".png" ;
instead of
String IntroImage1= "" + languageSelected + "/" + Intro1ImagePath + ".png" ;
Upd: Check values of languageSelected and Intro1ImagePath also.