set drawable image to imageview with a path sqlite - java

I would appreciate if you could help me with this, I'm trying to show pictures from my drawable folder, and the names of those images are in a sqlite database.
The problem is that I can't apply the image on the imageview. Any idea about why this happened. Thanks
This is my code
setviewholder.java
public class SetViewHolder extends RecyclerView.ViewHolder {
public ImageView txt_imagen;
public TextView txt_path_imagen;
public SetViewHolder (View itemView){
super(itemView);
txt_imagen = (ImageView) itemView.findViewById(R.id.txt_imagen);
txt_path_imagen = (TextView) itemView.findById(R.id.txt_path_imagen);
}
custom_list_item2.xml
<ImageView
android:id="#+id/txt_imagen"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
MyAdapter.java
public void onBindViewHolder(SetViewHolder holder, final int position){
holder.txt_path_imagen.setText(items.get(position).getPath_imagen());
Bitmap bmp = BitmapFactory.decodeFile("ar.com.allnight.allnight:drawable/"+items.get(position).getPath_imagen());
holder.txt_imagen.setImageBitmap(bmp);
the log:
Unable to decode the stream: java.ioFileNotFoundException: /ar.comm.allnight:drawable/imgage1
I have tried many different ways, but I canĀ“t make it work and the others vars shows ok!, So now it is everything else work ok.
Any idea about what I'm doing wrong?

Related

Short delay when replacing Image with Glide

I wan't to simply change an image from one to another image when the user clicks on it. The problem here is that there is a short delay for about 100 milliseconds when the first image gets replaced. It is pretty annoying because you can clearly see that the image dissapears for a short time when Glide loads Image2.
Here is how I preload the Image:
Glide
.with(mContext)
.load(imageURL)
.diskCacheStrategy(DiskCacheStrategy.DATA)
.preload();
And here is how I display the image:
Glide
.with(mContext)
.load(imageURL)
.diskCacheStrategy(DiskCacheStrategy.DATA)
.into(holder.itemImageView);
And here is my full RecyclerViewAdapter class so you can see how I preload and create the image:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private ArrayList<myItems> mMyItems;
Context mContext;
public MyAdapter(Context context, ArrayList<Item> myItems){
mContext = context;
mMyItems = myItems;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.my_item, parent, false);
return new MyViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull final MyViewHolder holder, int position) {
MyItem currentItem = mMyItems.get(position);
final String image1Url = currentItem.getImage1();
final String image2Url = currentItem.getImage2();
//Preload image1
Glide.with(mContext)
.load(image1Url)
.diskCacheStrategy(DiskCacheStrategy.DATA)
.preload();
//Preload image2
Glide.with(mContext)
.load(image2Url)
.diskCacheStrategy(DiskCacheStrategy.DATA)
.preload();
//Display image1
Glide.with(mContext)
.load(image1Url)
.diskCacheStrategy(DiskCacheStrategy.DATA)
.into(holder.itemImageView);
//item onClickListener
holder.itemImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Display image2
Glide.with(mContext)
.load(image2Url)
.diskCacheStrategy(DiskCacheStrategy.DATA)
.into(holder.itemImageView);
});
}
#Override
public int getItemCount() {
return mMyItems.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder{
ImageView itemImageView;
public MyViewHolder(#NonNull View itemView) {
super(itemView);
itemImageView = itemView.findViewById(R.id.imageViewItem);
}
}
}
The delay only occurs when clicking on the button for the first time, after that there is no delay, seems like the image is then cached. Does anyone know how to fix that?
The images are stored on my website as .webp files, I also tried .png but it doesn't work either.
When I call the images from the drawable folder in my project it works but that is not an option for me.
How can I fix this small delay when clicking the image the first time?
Do I preload the image wrong?
EDIT
Here is a new created project to demonstrate the issue so you can test it yourself (Delay only appears on the first click, restart app to reset the issue)
https://github.com/DaFaack/GlideDelayBugDemonstration
Maybe you need to add fade animation or if you want to change image sync, you need to get the bitmap. I think it's not a good way.
Glide V3
Bitmap myBitmap = Glide.with(applicationContext)
.load(yourUrl)
.asBitmap()
.into(500, 500)
.get();
imageView.setBitmap(myBitmap);
Glide V4
FutureTarget<Bitmap> futureBitmap = Glide.with(applicationContext)
.asBitmap()
.load(yourURL)
.submit();
Bitmap myBitmap = futureBitmap.get();
imageView.setBitmap(myBitmap);
Note: This code needs to be run in the background if you open strictMode.

need an example trying to use svg/png

This is my first post...im a beginner trying to make an app for android.
im trying to make a simple match game using a deck of playing cards. Ive been able to get open source svg and png playing cards online. They either come in individual cards images (in png or svg formats) or one image (svg or png) with every card spaced nicely with a contrast background.
With all of the research ive done, i try to utilize these svg or png files but run into either out of memory issues or very slow UX performance.
My goal is to get a gridview to show all 52 cards that swipe horizontally. and if i click on a card, and match its face...then i get a point.
heres my layout activty_play_board.xml
just have one item...a gridview:
<GridView
android:id="#+id/gridView"
android:layout_width="match_parent"
android:layout_height="540dp"
android:horizontalSpacing="2dp"
android:numColumns="4"
android:stretchMode="columnWidth"
android:verticalSpacing="2dp" />
I use this public class that i got from googles dev website. it allows me to fill in the gridview with the svg or png cards
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(200, 270));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setPadding(4,4, 4, 4);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
private Integer[] mThumbIds = {
R.drawable.two_of_clubs,
R.drawable.two_of_diamonds,
R.drawable.two_of_hearts,
R.drawable.two_of_spades,
R.drawable.three_of_clubs,
R.drawable.three_of_diamonds,
R.drawable.three_of_hearts,
R.drawable.three_of_spades,
R.drawable.four_of_clubs,
R.drawable.four_of_diamonds,
R.drawable.four_of_hearts,
R.drawable.four_of_spades,
R.drawable.five_of_clubs,
R.drawable.five_of_diamonds,
R.drawable.five_of_hearts,
R.drawable.five_of_spades,
R.drawable.six_of_diamonds,
R.drawable.six_of_hearts,
R.drawable.six_of_spades,
R.drawable.six_of_clubs,
R.drawable.seven_of_diamonds,
R.drawable.seven_of_hearts,
R.drawable.seven_of_spades,
R.drawable.seven_of_clubs,
R.drawable.eight_of_diamonds,
R.drawable.eight_of_hearts,
R.drawable.eight_of_clubs,
R.drawable.eight_of_spades,
R.drawable.nine_of_diamonds,
R.drawable.nine_of_hearts,
R.drawable.nine_of_spades,
R.drawable.nine_of_clubs,
R.drawable.ten_of_clubs,
R.drawable.ten_of_diamonds,
R.drawable.ten_of_hearts,
R.drawable.ten_of_spades,
R.drawable.jack_of_clubs,
R.drawable.jack_of_diamonds,
R.drawable.jack_of_hearts,
R.drawable.jack_of_spades,
R.drawable.queen_of_clubs,
R.drawable.queen_of_diamonds,
R.drawable.queen_of_hearts,
R.drawable.queen_of_spades,
R.drawable.king_of_spades,
R.drawable.king_of_clubs,
R.drawable.king_of_diamonds,
R.drawable.king_of_hearts,
R.drawable.ace_of_spades,
R.drawable.ace_of_diamonds,
R.drawable.ace_of_hearts,
R.drawable.ace_of_clubs
};}
heres my onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid_board);
Button goBackOnClick = (Button) findViewById(R.id.playBoardButton);
GridView gridview = (GridView) findViewById(R.id.gridView);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(newAdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(GridBoard.this, "Spot: " + position, Toast.LENGTH_LONG).show();
}
});
I have several questions.
How do i utilize the SVG file? am i calling the one big svg file and then referencing a specific location on the file when i call it for an imageview within my gridview?
i dont know how to use the one big SVG file, so ive tried using the individual svg images. but when i do that, my device is very laggy and slow. also sometimes the app crashes due to out of memory errors. Is there a special syntax to use when trying to reference a specific card within the large SVG file? Or do i have to manually edit the big svg file and get the smaller cards?
Is there a better way to do this than using a gridview?
any other general guidance would be greatly appreciated.
Thank you!
Heres my card sources:
one big palette
https://upload.wikimedia.org/wikipedia/commons/8/81/English_pattern_playing_cards_deck.svg
individual cards and one big palette
http://byronknoll.blogspot.com/2011/03/vector-playing-cards.html

Spinner updating only on user actions

I have IconText that has image and text views inside it, both class and xml.
I then populate the Spinner inside of the MainActivity with these IconTexts, using extended BaseAdapter (IconTextAdapter) as adapter.
Now, IconText works fine (shows as it should).
Spinner however doesn't.
When I start the app, it shows the first IconText as it should.
When I open the choose dialog, everything is showing as it should.
When I select another item, the choose dialog collapses and spinner displays no IconText (ie. only "arrow down" for opening choose dialog).
I can still open the choose dialog and choose another.
I've noticed that if I exit from app (return button, not really quiting the app) and enter again that the proper IconText is shown.
I guess that the fault lies with the adapter?
I will try avoiding posting a lot of code [:
IconText.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView android:id="#+id/it_image"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/home"/>
<TextView
android:id="#+id/it_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="20sp"
android:text="ABC"
android:textColor="#color/black_overlay"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
IconText.java
public class IconText {
static LayoutInflater inflator;
public ImageView icon;
public TextView text;
public View view;
public IconText(String title, int icon_id){
Log.d("IconText", "Create");
view = inflator.inflate(R.layout.icon_text, null);
text = (TextView) view.findViewById(R.id.it_text);
icon = (ImageView) view.findViewById(R.id.it_image);
text.setText(title);
icon.setImageResource(icon_id);
}
public static void initInflator(Context context){
if(inflator != null) return;
Log.d("IconText", "Init inflator");
inflator = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
}
IconTextAdapter.java
public class IconTextAdapter extends BaseAdapter implements SpinnerAdapter{
public ArrayList<IconText> items;
public IconTextAdapter(){
super();
items = new ArrayList<>();
}
#Override
public int getCount() {
return items.size();
}
#Override
public Object getItem(int arg0) {
return items.get(arg0);
}
#Override
public long getItemId(int position) {
return items.get(position).view.getId();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return items.get(position).view;
}
}
MainActivity, relevant code
void initFilter(){
Log.d("MainAction", "Find Filter");
filter = (Spinner) findViewById(R.id.filter);
Log.d("MainAction", "Create Adapter");
IconTextAdapter adapter = new IconTextAdapter();
Log.d("MainAction", "Create items");
for(int i=0; i<ETypes.names.length; i++){
IconText it = new IconText(ETypes.names[i], ETypes.icons[i]);
adapter.items.add(it);
}
Log.d("MainAction", "Setting adapter");
filter.setAdapter(adapter);
}
ETypes names[string] and icons[id] are static.
Inflator is initialized succesfully.
I haven't worked much in Android. If it were Java/Swing, I guess I would just call a redraw or something.
I know there are some bad practices here (all public variables, and so on). This code is still in early prototype stage, it will be fixed soon. I'm not looking for optimization, just for the solution to my problem.
Update 1:
So I saw I didn't implement getDropDownView so I did, the same code as getView (no need to post it?).
Also I made an experiment: At the end of IconText contrusctor I added
view = text
And it works just fine (showing only text).
I guess this pinpoints that the problem originates from custom view?
Update 2:
Did another experiment with IconText, setting view = icon; and it doesn't behave as it should, ie. it behaves like it's a custom view.
Doesn't really solve this specific bug, but it solves my problem.
Custom Image and Text View in Spinner Solution.
To update the list you must call
adapter.notifyDataSetChanged()
after adding new data to your adapter
Please refer to the following
https://developer.android.com/reference/android/widget/BaseAdapter.html#notifyDataSetChanged()

How to make bitmap(set in src) compress?

I wrote an universal custom ImageView , there are parts of code:
public class AvatarImageView extends ImageView {
#Override
protected void onDraw(Canvas canvas) {
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
// I want to make this bitmap compress
}
and then I can use this custom ImageView like this:
<CustomerView.AvatarImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:src="#drawable/avatar"
/>
It will lead to an oom error when I set more than 2 AvatarImageViews in my listview .
I see a lot of developers make it in that way: Google Training Loading Large Bitmaps Efficiently
BitmapFactory.decodeResource(res, resId, options);
It needs to offer a resId . But I want to reuse this custom ImageView ,so I couldn't offer a certain id .
Is there any good solutions ? Any help would be greatly appreciated.Thank you!

Fixing tiny images in GridView?

I'm trying to implement a basic GridView gallery as per the Android Developer Guide/Tutorial.
The ImageViews inside my grid are Bitmaps taken from a user's camera.
This works fine except for the fact that my images are incredibly small.
My xml:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>
My ImageAdapter
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Bitmap> imgs;
public ImageAdapter(Context c, ArrayList<Bitmap> arrayList) {
mContext = c;
imgs = arrayList;
}
public int getCount() {
return imgs.size();
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageBitmap(imgs.get(position));
return imageView;
}
}
I've tried various things to fix this issue, but the only one that seems to work is adjusting the ImageView's layout params to enormous numbers (e.g. GridView.LayoutParams(300, 300)).
However, even if I do this, my images are still relatively small (i.e. nowhere near 300dp x 300dp).
Any idea what I'm doing wrong?
Put this param in your ImageView.
android:adjustViewBounds="true"
You can see this too.
How to find cell width from StaggeredGridLayoutManager?
Setting layout params in my adapter to GridView.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)) managed to fix the issue.
Found this solution at GridView items are too small - Android
Try to change for bigger size:
android:columnWidth="90dp"
you should accept the answer of
android:adjustViewBounds="true"
What it means?it stretches your image to fit the height and width of your image view so they won't be small,but why you don't want to use
android:ScaleType="fitXY"?
Because first option adjusted the proportion of your image so it won't lose the quality and second one anyway stretches it and it loses quality!
IMPORTANT:be aware that if image will be too small,adjust option will not work as it must save the image proportion,so its all up to you what you want to use!
The problem is that new GridView.LayoutParams() expect args in pixels.
So, your need to convert 85 from dp to pixels first.

Categories

Resources