so in my app I need to save and to display an image on a image view, so i use SharedPreferences for saving it, the problem is that, if there is not a saved value, i want to display an image from drawables, and for that I´m using this code:
final ImageButton imgView = (ImageButton) findViewById(R.id.UserImageButton);
String photo = PreferenceManager.getDefaultSharedPreferences(UserActivity.this).getString("Image", String.valueOf(getResources().getDrawable(R.drawable.user, getApplicationContext().getTheme())));//NameOfTheString, StringIfNothingFound
imgView.setImageBitmap(BitmapFactory.decodeFile(photo));
But if there is no image saved, the drawable is not used on the ImageView, and it doesn´t show anything.
How could I fix it?
There's no requirement to use the default value of your getString() method as a direct path to your drawable. You could simply do the following:
private static final String IMAGE_PREF = "image_pref_key";
private static final String PREF_FAIL_CODE= "no_image_found";
String photo = PreferenceManager.getDefaultSharedPreferences(UserActivity.this).getString(IMAGE_PREF, PREF_FAIL_CODE);
if (photo.equals(PREF_FAIL_CODE) {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.user));
}
When you are getting the string from default shared preferences, if the value is not stored, set the default value returned to something e.g. "not found"
Now before setting doing setImageBitmap check if the value in your String variable photo is "not found". If it is display the drawable from your resource. Else display your saved image after decoding it as your are doing it currently.
Related
In my Android App, the Captured image along with its details is stored and displayed in a recycler View. The Details are name and description of the picture. I'm storing the details along with Storage path(mCurrentpath) of the captured image in a room database table. I'm able to retrieve each row of the database and display it in recyclerview item. The problem is - if I try to save only the details without any picture captured,the item displays an empty imageview along with details. Instead of displaying empty image view i need to display an default image.Kindly help
Note: i also tried to save a default string in database if image not captured. and checked With if() statement in onBindViewHolder
#Override
public void onBindViewHolder(#NonNull ImageHolder imageHolder, int i) {
ImageEntry currentImageEntry = imageEntries.get(i);
String storedAddress = currentImageEntry.getImageStoredAddress();
if(storedAddress != "Address is null") {
imageHolder.imageView.setImageBitmap(BitmapFactory.decodeFile(currentImageEntry.getImageStoredAddress()));
}else {imageHolder.imageView.setImageResource(R.drawable.default_image);}
imageHolder.textViewTpye.setText(currentImageEntry.getPropertyType());
imageHolder.textViewDesc.setText(currentImageEntry.getProprtyDescription());
}
thanks
use equals for comapring string value
if (storedAddress.equals("Address is null")) {
imageHolder.imageView.setImageResource(R.drawable.default_image);
} else {
imageHolder.imageView.setImageBitmap(BitmapFactory.decodeFile(currentImageEntry.getImageStoredAddress()));
}
Note : == tests object references, .equals() tests the string values.
for Example,
String a="test me";
String b="test me";
a == b //this will return false
a.equals(b) //this will return true
Just Place a default image in XML file and change this image
if
if(storedAddress != "Address is null")
or If no image is captured , store default image address in database.
I have a string with the name of the file that I want (R.drawable.square) to put in the imageview.
String shape = "square"
I am trying to use the method below to show the image
imageView.setImageResource()
Hardcoding the filename in works but I want to be able to pass different filenames in.
image.setImageResource(R.drawable.square)
tl;dr
can't get from String:"square" to int:R.drawable.square
Please see Android, reference things in R.drawable. using variables?
//to retrieve image in res/drawable and set image in ImageView
String imageName = "picture"
int resID = getResources().getIdentifier(imageName, "drawable", "package.name");
ImageView image;
image.setImageResource(resID );
I achieved the effect by putting the images in the assets directory
I think it's better to use you Strings as key and your resource ids as value in a Map Object.
Map<String, Integer> resources = new HashMap<>();
resources.put("square", R.drawable.square);
// resources.put("...", R.drawable....);
imageView.setImageResource(resources.get("square"));
I'm sorry for my English .
I have a SQLite database of stored name of images in the drawable folder.
Example : R.drawable.image1 , R.drawable.image2 , ...
Is there a way that addresses stored in the database attributed to ImageViews?
Yes, you can get a Drawable by it's name, but it needs to be the full name of the resource, including the extension. So, for instance, if "R.drawable.image1" refers to an image named "image1.png", you will need to look it up by the string "image1.png".
Given the name of the image (e.g. "image1"), you can use the following method to get the resource id of the image (assuming that all of your images have the ".png" extension):
private final Map<String, Integer> iconMap = new HashMap<>();
public int getIconResourceId(final String imageName) {
String name = imageName.replace(".png", "");
Integer resourceId = iconMap.get(name);
if(resourceId == null) {
Resources resources = getResources();
resourceId = resources.getIdentifier(name, "drawable", getPackageName());
iconMap.put(name, resourceId); // cache the entry
}
return resourceId.intValue();
}
Note that the above code caches the resource ids - I use this in code for a RecyclerView, where the same image may need to be retrieved often, while scrolling the RecyclerView. If you don't need that, you may want to remove the HashMap.
In my application I am getting the filename dynamically from database but I'm not sure how to update the image of ImageView.
Let's say I have this code:
String str = "image2.jpg";
ImageView imageView = (ImageView) findViewById(R.id.image);
How could I set the image to be image2.jpg, if current image is another?
PS. In my application, I have a spinner. Every time the selected element is changed, I make a database request and I get the image name, and I need to modify image of imageview dynamically.
write a method like this in your Activity
public Drawable getImage(String name) {
return getResources().getDrawable(
getResources().getIdentifier(name, "drawable",
getPackageName()));
}
pass you image name here you will get drawable object then set it to Imageview
Drawable image = getImage(name);
imageView.setImageDrawable(image);
I want to have an image sent from recipe_button_list.java to recipe_display_screen.java,
and my current code highlights an error in my code...
This is how image is being sent in recipe_button_list:
Intent i= new Intent(getBaseContext(),recipedisplayscreen.class);
//Sending data to the next screen
i.putExtra("textView1", inputIngredients1.getText().toString());
i.putExtra("textView2", inputMethod1.getText().toString());
i.putExtra("image_string",R.drawable.blustudios);
Log.e("n", inputMethod1.getText()+"."+ inputIngredients1.getText());
This is how image is recieved in recipe_display_screen:
Intent i = getIntent();
String Ingredients = i.getStringExtra("textView1");
String Method = i.getStringExtra("textView2");
String RecipeImage = i.getStringExtra("image_string");
And this is how it is set (gives error(highlights setImageResource)
MethodDisplay.setText(Method);
IngredientsDisplay.setText(Ingredients);
RecipeDisplay.setImageResource(RecipeImage);
What is my error???
Thanks in advance :P
R.drawable.blustudios is not a String. The auto-generated R.java class contains integers which are actually resource ids.
Change this line...
String RecipeImage = i.getStringExtra("image_string");
...to this...
int RecipeImage = i.getIntExtra("image_string", 0);
You can't send image as string.
You can either use Serialized or Parcable.
I know bitmap is already parcable, maybe ImageView is parcable too check it out.
In case it's not, just save the ImageView as a bitmap and send it.
Whatever the case use setParcableExtra() and same with get.
You clearly don't understand the concept of passing data between activites.
Check out This question, you will probably figure out the answer from there.