Commit 7c44ea7f authored by Lucas NAURY's avatar Lucas NAURY

Fix nullable image + image size

parent 3f6a5a92
......@@ -30,7 +30,7 @@ public class DBHelper extends SQLiteOpenHelper {
// Creating table query
private static final String CREATE_TABLE = "create table " + TABLE_NAME + "(" + _ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + TITLE + " TEXT NOT NULL, " + ADDRESS + " TEXT, " + IMAGE + " BLOB, " + PRICE + " DOUBLE, " + DESCRIPTION + " TEXT);";
+ " INTEGER PRIMARY KEY AUTOINCREMENT, " + TITLE + " TEXT NOT NULL, " + ADDRESS + " TEXT NOT NULL, " + IMAGE + " BLOB, " + PRICE + " DOUBLE NOT NULL, " + DESCRIPTION + " TEXT);";
public DBHelper(Context context) {
super(context,
......
package com.example.tpleboncoin.models;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Parcel;
import android.os.Parcelable;
import androidx.annotation.NonNull;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
public class Annonce implements Parcelable {
static int nbAnnonces = 0;
private int id;
......@@ -38,6 +43,17 @@ public class Annonce implements Parcelable {
public double getPrix(){return prix;}
public String getAdresse(){return adresse;}
public byte[] getImage(){return image;}
public Bitmap getImageAsBitmap(){
byte[] img = this.getImage();
if(img == null){
return null;
}
InputStream is = new ByteArrayInputStream(img);
Bitmap bmpImage = BitmapFactory.decodeStream(is);
return bmpImage;
}
public String getDescription(){return description;}
......@@ -54,8 +70,11 @@ public class Annonce implements Parcelable {
parcel.writeDouble(prix);
parcel.writeInt(image.length);
int imageLength = image != null ? image.length : 0;
parcel.writeInt(imageLength);
if(imageLength>0){
parcel.writeByteArray(image);
}
parcel.writeString(description);
parcel.writeInt(id);
......@@ -76,8 +95,15 @@ public class Annonce implements Parcelable {
prix = in.readDouble();
image = new byte[in.readInt()];
int imageLength = in.readInt();
if(imageLength > 0){
image = new byte[imageLength];
in.readByteArray(image);
}else{
image = null;
}
description = in.readString();
id = in.readInt();
......
......@@ -36,7 +36,11 @@ public class DetailScreen extends AppCompatActivity {
binding.descriptionTextView4.setJustificationMode(LineBreaker.JUSTIFICATION_MODE_INTER_WORD);
}
binding.titreTextView.setText(annonce.getTitre());
binding.imageView.setImageURI(Uri.parse("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQuN0ilDzXue08UjwoRi4jiqj0Y9FWs7jDhYY949clvcVYpoCUZzH1OkTKv5-FqBHDwKG0&usqp=CAU"));
if(annonce.getImage() != null){
binding.imageView.setImageBitmap(annonce.getImageAsBitmap());
}
}
binding.smsButton.setOnClickListener(new View.OnClickListener() {
@Override
......
......@@ -3,6 +3,7 @@ package com.example.tpleboncoin.ui.ajout_annonce;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
......@@ -73,10 +74,16 @@ public class AjoutAnnonceFragment extends Fragment {
}
// On convertit l'image en byte array
byte[] imageInByte = null;
if(imageAnnonce.getVisibility() == View.VISIBLE){
Bitmap bitmap = ((BitmapDrawable) imageAnnonce.getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap = resizeBitmap(bitmap, 500);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageInByte = baos.toByteArray();
imageInByte = baos.toByteArray();
}
// On créé l'objet de la nouvelle annonce
Annonce nouvelleAnnonce = new Annonce(titreAnnonce.getText().toString(), adresseAnnonce.getText().toString(), imageInByte, Double.parseDouble(prixAnnonce.getText().toString()), descriptionAnnonce.getText().toString());
......@@ -157,6 +164,26 @@ public class AjoutAnnonceFragment extends Fragment {
// Gestion des images
private Bitmap resizeBitmap(Bitmap image, int maxSize){
// Don't resize if already small image
if(image.getWidth()<maxSize && image.getHeight()<maxSize){
return image;
}
int width = image.getWidth();
int height = image.getHeight();
float bitmapRatio = (float)width / (float) height;
if (bitmapRatio > 1) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}
private void choisirImageDansGalerie(){
// Create an instance of the Intent of the type image
Intent i = new Intent();
......
......@@ -106,15 +106,17 @@ public class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.ViewHolder> {
Log.d(TAG, "Element " + position + " set.");
//Byte array to img
InputStream is = new ByteArrayInputStream(mDataSet.get(position).getImage());
Bitmap bmpImage = BitmapFactory.decodeStream(is);
if(mDataSet.get(position).getImage() != null){
Bitmap bmpImage = mDataSet.get(position).getImageAsBitmap();
viewHolder.getImageView().setImageBitmap(bmpImage);
}
// Get element from dataset at this position and replace the contents of the view
// with that element
viewHolder.getTitreTextView().setText(mDataSet.get(position).getTitre());
viewHolder.getAdresseTextView().setText(mDataSet.get(position).getAdresse());
viewHolder.getPrixTextView().setText(String.format("%,.2f €", mDataSet.get(position).getPrix()));
viewHolder.getImageView().setImageBitmap(bmpImage);
}
// Return the size of dataset (invoked by the layout manager)
......
......@@ -50,7 +50,8 @@
app:layout_constraintEnd_toStartOf="@+id/smsButton"
app:layout_constraintHorizontal_bias="0.46"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_launcher_background" />
<TextView
android:id="@+id/titreTextView"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment