Why won't my images not load in Recyclerview - android

Glide won't load the images I have linked with the products on my recycler view. The images should be pulled from my PHP MySQL database. Not really sure what I did wrong. Please help
I've tried a few solutions I found on the web but none of them worked for me.
private String image;
The above is the string identifier from my product list
//loading the image
Glide.with(mCtx)
.load(product.getImage())
.into(holder.imageView);
holder.textViewPlateNumber.setText(product.getPlatenumber());
holder.textView1.setText(product.getMake());
holder.textView2.setText(product.getModel());
holder.textView3.setText(product.getYear());
holder.textViewName.setText(product.getName());
holder.textDate.setText(product.getDate());
holder.recyclerid.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String platenumber = productList.get(position).getPlatenumber();
String make = productList.get(position).getMake();
String model = productList.get(position).getModel();
String year = productList.get(position).getYear();
String name = productList.get(position).getName();
String date = productList.get(position).getDate();
String vin = productList.get(position).getVin();
String displacement = productList.get(position).getDisplacement();
String fueltype = productList.get(position).getFueltype();
String transmission = productList.get(position).getTransmission();
String mileage = productList.get(position).getMileage();
String ownerorcompany =
productList.get(position).getOwnerorcompany();
String homeorcompanyaddress =
productList.get(position).getHomeorcompanyaddress();
String contactnumber =
productList.get(position).getContactnumber();
String emailaddress = productList.get(position).getEmailaddress();
String facebook = productList.get(position).getFacebook();
String image = productList.get(position).getImage();
The above is the code I used to show the images of each item off my adapter.
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/imageView"
android:layout_width="0dp"
android:layout_height="0dp"
android:src="#drawable/car_avatar"
app:civ_border_color="#color/cta"
app:civ_border_width="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/guideline17"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
The above is how the image should be laid out in my xml file. The rest of the items saved on my php sql show up on the list except the images. Here is what my php file looks like:
$vehicle = array();
while ($row=mysqli_fetch_array($sql)) {
$temp = array();
$temp['id'] = $row['VehicleID'];
$temp['platenumber'] = $row['PlateNumber'];
$temp['make'] = $row['Make'];
$temp['model'] = $row['Model'];
$temp['year'] = $row['Year'];
$temp['name'] = $row['OwnerorCompany'];
$temp['date'] = $row['AddDate'];
$temp['image'] = $row['vehicleImage'];
$temp['vin'] = $row['Vin'];
$temp['displacement'] = $row['Displacement'];
$temp['fueltype'] = $row['FuelType'];
$temp['transmission'] = $row['Transmission'];
$temp['mileage'] = $row['Mileage'];
$temp['ownerorcompany'] = $row['OwnerorCompany'];
$temp['homeorcompanyaddress'] = $row['HomeorCompanyAddress'];
$temp['contactnumber'] = $row['ContactNumber'];
$temp['emailaddress'] = $row['EmailAddress'];
$temp['facebook'] = $row['FacebookID'];
array_push($vehicle, $temp);
}
echo json_encode($vehicle);
//}
?>
And this is how the images are saved on my database:
$photo = $_POST['photo'];
$id=uniqid();
$path = "vehicle_upload/$id.jpeg";
$finalpath = "http://192.168.0.10/widevalueautoInc
2/server/api/".$path;
When I run the app, all goes well except the images not showing up on the list. This is the java code I used to save the image and the size.
private void addVehicle(String stringImage) {
}
public String getStringImage(Bitmap bitmap){
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] imageByteArray = byteArrayOutputStream.toByteArray();
String encodedImage = Base64.encodeToString(imageByteArray,
Base64.DEFAULT);
return encodedImage;
}
}

remove circular imageview and use default image view, to use circle crop .. use Glide circle crop function....
Glide.with(context).load("dddd").apply(RequestOptions.circleCropTransform()).into(imageview);

Just make a common method as below in your utility class as below :
public static void setCircularImageToGlide(final Context context, final CircularImageView imageView, String imageUrl) {
Glide.with(context).load("" + imageUrl).asBitmap().placeholder(R.drawable.ic_photo_placeholder).transform(new CircleTransform(context)).into(new BitmapImageViewTarget(imageView) {
#Override
protected void setResource(Bitmap resource) {
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(context.getResources(), resource);
circularBitmapDrawable.setCircular(true);
imageView.setImageDrawable(circularBitmapDrawable);
}
});
}
I have passed CircularImageView as an argument to method single you are using CircularImageView instead of simple ImageView.
Call it as below wherever you need to set image in to your CircularImageView as below :
CommonUtil.setCircularImageToGlide(YOUR_CONTEXT, YOUR_CIRCULAR_IMAGE_VIEW, "" + YOUR_IMAGE_URL);
Am using below dependency :
implementation 'com.github.bumptech.glide:glide:3.7.0'

Related

Picasso, image not shown

I am using a Class that implements GoogleMap.InfoWindowAdapter.
I want to show an image loaded from internet inside the info window:
#Override
public View getInfoContents(final Marker m) {
//Carga layout personalizado.
View v = inflater.inflate(R.layout.info_windows_origen, null);
String info = m.getTitle();
String direccion = m.getSnippet();
String url = m.getSnippet();
((TextView)v.findViewById(R.id.info_window_nombre)).setText("PUNTO DE PARTIDA");
((TextView)v.findViewById(R.id.info_window_placas)).setText(info);
((TextView)v.findViewById(R.id.info_window_estado)).setText(direccion);
ImageView imgProfile =(ImageView)v.findViewById(R.id.info_window_imagen);
SharedPreferences prefs =
getApplicationContext().getSharedPreferences(MISDATOS, Context.MODE_PRIVATE);
String imagen = prefs.getString("imagen", "por_defecto#email.com");
Log.d("BOTON ORIGEN ","mi imagen "+urlProfileImg+imagen);
Picasso.with(getApplicationContext()).load(urlProfileImg+imagen).fit().into(imgProfile);
return v;
}
I have checked both, the image URL and the image name, and both are correct, but the image is not shown.
Be sure you have internet permission in the manifest.
Try removing fit
Posting the url you are providing would be easier to work with

How to display image from sql database to listview in android xamarin using webservices and json?

How to display image from sql database to listview in android xamarin using webservices and json?
public override View GetView(int position, View convertView, ViewGroup parent)
{
var item = objList[position];
if (convertView == null)
{
convertView = objActivity.LayoutInflater.Inflate(Resource.Layout.ContListViewHospName, null);
}
convertView.FindViewById<TextView>(Resource.Id.tvHospID).Text = item.HospID;
convertView.FindViewById<TextView>(Resource.Id.tvHospName).Text = item.HospName;
byte[] img = (byte[])item.HospLogo;
Bitmap bitmap = BitmapFactory.DecodeByteArray(item.HospLogo, 0, item.HospLogo.Length);
convertView.FindViewById<ImageView>(Resource.Id.imgLogo).SetImageBitmap(bitmap);
return convertView;
}
i store binary value in one string variable then i use json to transfer values to my cs file in my android project
Your store binary value in one string should looks like this: "iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAABHNCSVQICAgIfAhkiAAAIABJREFUeJzsvfd33EiW7/mJgEtHb0WKFGWqSuW96epqM9P9+s30mZ/2j33n7O7Zt/Nme8zr6emu6i5fk........"
That is base64 bytes. You can not just use (byte[]) to convert the string to byte[]:
you need to use System.Convert.FromBase64String() function.
//The string is download form server in the json file
string myBytes = "iVBORw0KGgoAAAANSUhEUgAAAgAAAAIACAYAAAD0eNT6AAAABHNCSVQICAgIfAhkiAAAIABJREFUeJzsvfd33EiW7/mJgEtHb0WKFGWqS........"
byte[] decByte3 = System.Convert.FromBase64String(myBytes);
And then convert the bytes to image:
public Bitmap Bytes2Bimap(byte[] b)
{
if (b.Length != 0)
{
return BitmapFactory.DecodeByteArray(b, 0, b.Length);
}
else
{
return null;
}
}
and convert the bitmap to drawable :
Bitmap myIcon = Bytes2Bimap(decByte3);
Drawable myIconD = new BitmapDrawable(myIcon);
imageView1.Background = myIconD;
May be your image is too big try to scale it, like here
Also make sure you have set proper dimension in your layout like width and height.

Compare 2 images appium [duplicate]

I am able to successfully take the screenshot one of the page of my application JainLibrary using below code. I am using junit and appium.
public String Screenshotpath = "Mention the folder Location";
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(Screenshotpath+"Any name".jpg"));
Now I want to compare the screenshot with a reference image so that I can move forward with the test case.
A simple solution would be to compare each pixel with the reference screenshoot:
// save the baseline screenshot
driver.get("https://www.google.co.uk/intl/en/about/");
File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\\temp\\screenshot.png"));
// take another screenshot and compare it to the baseline
driver.get("https://www.google.co.uk/intl/en/about/");
byte[] pngBytes = ((TakesScreenshot)driver).getScreenshotAs(OutputType.BYTES);
if (IsPngEquals(new File("c:\\temp\\screenshot.png"), pngBytes)) {
System.out.println("equals");
} else {
System.out.println("not equals");
}
public static boolean IsPngEquals(File pngFile, byte[] pngBytes) throws IOException {
BufferedImage imageA = ImageIO.read(pngFile);
ByteArrayInputStream inStreamB = new ByteArrayInputStream(pngBytes);
BufferedImage imageB = ImageIO.read(inStreamB);
inStreamB.close();
DataBufferByte dataBufferA = (DataBufferByte)imageA.getRaster().getDataBuffer();
DataBufferByte dataBufferB = (DataBufferByte)imageB.getRaster().getDataBuffer();
if (dataBufferA.getNumBanks() != dataBufferB.getNumBanks()) {
return false;
}
for (int bank = 0; bank < dataBufferA.getNumBanks(); bank++) {
if (!Arrays.equals(dataBufferA.getData(bank), dataBufferB.getData(bank))) {
return false;
}
}
return true;
}
Note that you need to save the reference screenshot as a PNG. A JPEG format will alter the pixels.

Saving an Object containing Bitmap

I'm working on an android application (first application-beginner) and I'm trying to save data when the app closes to load again.
The data I want to store is a list of books, and each book contains info (author, date, etc) and a bitmap containing the book's picture. I tried using GSON to convert the list to JSON and store in SharedPreferences but that caused problems because of the bitmap.
How should I save the file and retrieve it again when the app launches ?
This is a brief version of the code
Library Class
public class Library {
private ArrayList<Entry> library ;
public Library () {
library = new ArrayList<Entry>();
}
public void addEntry( Entry entry ) {
library.add(entry);
}
public void removeEntry ( Entry entry ) {
if (library.contains(entry))
library.remove(entry);
else Log.d ( "Library" , "Entry Not Found");
}
public ArrayList<Entry> getLibrary() {
return library;
}
#Override
public String toString() {
return "Library{" +
"library=" + library +
'}';
}
}
Entry Class
public class Entry {
Book book ;
final LocalDate borrowDate;
LocalDate dueDate;
//some methods for application
}
Book Class
public class Book implements Parcelable {
private String title;
private String author;
private String isbn ;
private double rating;
private int ratingCount;
private int pageCount;
private transient Bitmap image;
private String overview;
//some methods
}
Don't put the bitmap in sharedpreferences. Save it in file.
If you need to persist the bitmap, you can assign it to a static field.
You can also convert it to a 64 bit String.. but that is bad design (and I think it is a very slow and expensive operation!):
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
String encoded = Base64.encodeToString(b, Base64.DEFAULT);
^ Save that to SharedPreferece. Now to decode:
byte[] imageAsBytes = Base64.decode(encoded.getBytes());
ImageView image = (ImageView)this.findViewById(R.id.ImageView);
image.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
Use Picasso and load the urls directly instead of downloading and saving as bitmap
Picasso.with(mContext)
.load("imageURL here")
.placeholder(R.drawable.default_pic)
.error(R.drawable.error_pic)
.resizeDimen(100, 100)
.centerCrop()
.into(holderOrderHistory.mIcon);

Load json data in Android-uitableview

Help please!
In my application I use Android-UiTableView ( thiagolocatelli / android-uitableview ) and AQuery ( androidquery / androidquery )
My task is to make the loading of data from json array and load image from URL.
Load a list without pictures I was able to:
jgall = json.getJSONArray("gall");
int z = 0;
for(int i = 0; i < jgall.length(); i++){
JSONObject c = jgall.getJSONObject(i);
tableView.addBasicItem(c.getString("name"), c.getString("place"));
}
I try to show a customized view and nothing happens
LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout view = (RelativeLayout) mInflater.inflate(R.layout.custom_view,null);
String iurl = c.getString("img");
aq.id(R.id.img).image(iurl, false, true);
ViewItem viewItem = new ViewItem(view);
tableView.addViewItem(viewItem);
Tell me it's real to make using Android-UiTableView? if so! how?
I have done this by modifying the library code myself.
I have added the following constructor at UITableView.java:
public void addBasicItem(Drawable drawable, String title, String summary) {
mItemList.add(new BasicItem(drawable, title, summary));
}
And the following at BasicItem.java:
public Drawable getDDrawable() {
return dDrawable;
}
public BasicItem(Drawable ddrawable, String _title, String _subtitle) {
this.dDrawable = ddrawable;
this.mTitle = _title;
this.mSubtitle = _subtitle;
}
And once again modified the setupBasicItem method at UITableView.java file:
((ImageView) view.findViewById(R.id.image)).setBackgroundDrawable(item.getDDrawable());
Finally, at your code add the following:
URL newurl = new URL("http://www.example.com/example.png");
Bitmap bmp = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream());
Drawable drawable = new android.graphics.drawable.BitmapDrawable(getResources(),bmp);
tableView.addBasicItem(drawable, "Title", "SubTitle");
Hope it helps, cause it works for me.

Categories

Resources