I am developing a small online shopping application in Android which needs to have lot of items images, I have a web service which gives the path of image present in android drawable-mdpi, My problem here that I tried this code to get the imageresource id, but it always gives me 0.
String uri = "#drawable/bangle1.png";
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
Is there any method, to get resource id, so that I can use it for drawable like :
Drawable res = getResources().getDrawable(imageResource);
I tried googling but most of them suggested the above method and this gives me always 0 value. Any one please help here.
It will be better to get your images into /assets/ folder, and don't use drawable resources for that. Because Note: use of this function is discouraged. It is much more efficient to retrieve resources by identifier than by name.
If you still want to use your method try
getResources().getIdentifier("bangle1.png", "drawable", getPackageName());
Thanks to all , even i tried it all suggested answers by others , it didnt resolve but i found finally the solution here: How do I get the resource id of an image if I know its name?.
it worked magically:
String mDrawableName = "bangle1";
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
Here , myDrawableName should not have extension like : ".png". Thanks to all.
Your problem is that you're writing the image name with its suffix. Try using this code:
int imageResource = getResources().getIdentifier(
"your image name without suffix(bangle1)", "drawable", YourActivtyName.this);
Related
As known, ALBUM_ART was deprecated in Android 11. Google says that ContentResolver.loadThumbnail should be used instead. But i totally don't understand how to use it, especially what i should provide as first parameter, uri. Documentation says:
Uri: The item that should be visualized as a thumbnail. This value cannot be null.
What is that item and how can I get it? Is this Uri of the music file?
You need to pass in the Uri of the track.
//This will get you the uri of the track, if you already have the track id
Uri trackUri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, trackId);
Bitmap bm = contentResolver.loadThumbnail(trackUri, new Size(512,512), null);
Set the bitmap to an ImageView
imageView.setImageBitmap(bm);
EDIT:
I asked a similar question a day before and I wrote this post based on information I gathered since that question -
First Cache Question. I know they are similar but this question is more concise to avoid extra information. I also didn't want to delete that post since it was answered, even though the answer didn't suffice.
I use a recyclerview to show photos I get from google places api. I know that I am not allowed to legally cache photos outside of runtime. I didn't know that was happening until I randomly opened my phone's gallery to see a lot of copies of the photos I get from google.
My first guess was that my use of Picasso was the issue so I added code to fix it,
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.networkPolicy(NetworkPolicy.NO_CACHE)
finding these in StackOverflow was pretty simple, but this didn't fix anything except that now it seems to only download them once especially when I delete them. I believe I eliminated the possible issues and am outlining the last one in this question.
private Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
#Override
public void onBindViewHolder(#NonNull VenueViewHolder venueViewHolder, int i) {
Collections.sort(vIAL, (o1, o2) -> o1.getVenueName().compareToIgnoreCase(o2.getVenueName()));
VenueItem currentItem = vIAL.get(i);
Picasso.with(context)
.load(getImageUri(context, currentItem.getVenueImage()))
.fit()
.memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
.networkPolicy(NetworkPolicy.NO_CACHE)
.into(venueViewHolder.vIV);
venueViewHolder.vTV.setText(currentItem.getVenueName());
Log.i(TAG, "The photo should have been on screen");
}
The URI method getImageUri is something I found as an answer to another problem I had, that I needed a URI to implement the Picasso library so that I can manipulate the photos before displaying them.
My question is - How do I remove the photos when the app closes?
UPDATE:
I changed my tactic to see what would happened and used Glide
#Override
public void onBindViewHolder(#NonNull VenueViewHolder venueViewHolder, int i) {
Collections.sort(vIAL, (o1, o2) -> o1.getVenueName().compareToIgnoreCase(o2.getVenueName()));
VenueItem currentItem = vIAL.get(i);
Glide.with(context)
.load(currentItem.getVenueImage())
.into(venueViewHolder.vIV);
venueViewHolder.vTV.setText(currentItem.getVenueName());
}
and it gave a fatal Error
E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 4344032)
This was one of the errors and it didn't occur the first time I ran this new code but it got worse the second and third time I ran it.
I shifted my code based on an answer I got early from #Padmini S but they used a url in the load part and I pass a bitmap because for the life of me I can't figure out how to get a URL from Google Places API instead of the code they provide in
Google Place Photos.
I'm relatively new to coding so this is me trying to piece together what I need to learn more. I'm just out of ideas of what to search for so I ask here,
based on the new information I gathered from the answer, can I replace
MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
in my code so that my photos don't get saved to my phone's photos?
Final Update
I ended up rewriting a lot of the code surrounding this and deleting this code. I ended up making an arraylist class to hold the array list for the duration of runtime and it let me remove most of the extra code I wrote out of ignorance.
I really don't know whether it'll solve your problem or not , but you are storing the images in device local storage here,
private Uri getImageUri(Context inContext, Bitmap inImage) {
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
}
I have never used google places api but usually apis will return you with an image url you can store that url in POJO class and display it directly in recyclerview row image view like I am doing in below code,
public void onBindViewHolder(#NonNull ViewHolder viewHolder, final int i) {
viewHolder.description.setText(links.get(i).getTitle());
Glide.with(context).load(links.get(i).getImage_url()).into(viewHolder.image);
}
I've started to develop an Android app and I'm very new about this.
My app has to read some QR code and at the moment I'm using the zxing library.
Now I've got some issue into a part of the application that involves playing sounds stored into the /res/raw:
my code sometimes runs.. but most of times it doesn't
I can access some sound files in raw folder with this code:
var myPlayer = MediaPlayer.create(this, R.raw.filename)
but not calling the file I need through a variable:
var myVarWithFilenameInside: String = "filename"
var myFileInt: Int = resources.getIdentifier("$myVarWithFilenameInside", "raw", this.getPackageName())
var myPlayer = MediaPlayer.create(this, myFileInt)
(even if apparently at "random" this seems to occasionally work; it's pretty strange indeed..)
I tried already to not specify the type of variable and also to insert packageName (as suggested by the IDE) instead of this.getPackageName()... However nothing seems to work
PS.
also this instruction works perfectly, but again I have to obtain the identifier without using a variable but putting it inside the method manually
var myFileInt: Int = resources.getIdentifier("filename", "raw", this.getPackageName())
var myPlayer = MediaPlayer.create(this, myFileInt)
I've got very stuck with this... So any kind of help will be appreciated
Thanks in advance
Assuming there is a file filename.mp3 in the raw folder, this is working for me:
var myVarWithFilenameInside = "filename"
var myFileInt: Int = resources.getIdentifier(myVarWithFilenameInside, "raw", this.packageName)
var myPlayer: MediaPlayer = MediaPlayer.create(this#YourActivity, myFileInt)
myPlayer.start()
Hi everybody I've got the following problem with an adroid app: I want to read an image saved and perform some operations on it.
(I do not need to use this image to draw something on screen, I just have to check the color of some pixels)
I'm using the following code:
Bitmap bitmap = BitmapFactory.decodeFile("drawable-v24/ean13.bmp");
The fact is that the BitmapFactory returns null as a FileNotFoundException is thrown. (by now the file is saved inside res folder)
I don't really understand where I should put the image and how to give the path to the BitmapFactory to be able to get it properly.
(I guess the problem is due to the fact that the image actually is stored on my pc and not on the Android device but I can't understand how should I proceed)
Thank you in advance for the help! :)
If you are accessing image from resource drawable of mipmap. You should use BitmapFactory.decodeResource to get Bitmap.
Bitmap bitmap=BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
Or another overloaded version .
public static Bitmap decodeResource(Resources res, int id, Options opts)
where getResources() is method of Activity.
I am trying to set my image in my ImageView through a variable (item is the variable name, which I will be setting to lowerCase).
However, it is not working and resID will return 0.
Here is my code:
ImageView iv = (ImageView)v.findViewById(R.id.imgView);
if (item != null) {
Log.i("item: ", item.toLowerCase());
int resID = getResources().getIdentifier(item.toLowerCase(), "drawable", "package.name");
Log.i("resid: ", String.valueOf(resID)); //will return 0 - im guessing this is where the problem is
iv.setImageResource(resID);
}
I used this as a guide Android, reference things in R.drawable. using variables? but none of the other solutions work for me, in fact they all gave me the same result (returning 0).
I can provide more code if needed (just didn't want it to be bombastic).
Thanks in advance!
getIdentifier() Returns 0 if no such resource was found. (0 is not a valid resource ID.)
and you should getPackageName() method of the context object which always return the correct package name.
if you will show your folder stracture and file's names i can help you to fix it.
btw, if you can get the drawable directly from the resource you can just use R.drawable.resName