master123
master123

Reputation: 37

How to display the image from database in android imageview

Hi In My application I am getting the json response in that image is there.That image I want to display in android imageview.In that image I want to set for imageView.

picture":"547150_156322317826149_1332901104_n.jpg

Can any one help me

Upvotes: 3

Views: 22636

Answers (2)

Fahad Bin Asif
Fahad Bin Asif

Reputation: 196

You can use Glide to load image into your ImageView e.g.

Glide.with(context).load(imagePath).into(imageView);

imagePath could be Remote URL, Drawable asset, Bitmap, File Uri etc.

In your case you are loading picture name received in json response you need to attach BASE URL with picture variable to make complete path to the image

Upvotes: 0

Mukesh Soni
Mukesh Soni

Reputation: 1

RecyclerViewAdapter.java

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder> {
Context context;
ArrayList<DataList> arrName;

public RecyclerViewAdapter(Context context, ArrayList<DataList> arrName) {
    this.context = context;
    this.arrName = arrName;
}


@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.name_row, viewGroup, false));
}

@Override
public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
    viewHolder.name.setText(arrName.get(i).getName());
    viewHolder.address.setText(arrName.get(i).getAddress());
    DataList datalist = arrName.get(i);

    byte[] recordImage = datalist.getImage();
    Bitmap bitmap = BitmapFactory.decodeByteArray(recordImage, 0, recordImage.length);
    viewHolder.img.setImageBitmap(bitmap);
}

@Override
public int getItemCount() {
    return arrName.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
    TextView name, address;
    ImageView img;

    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        name = itemView.findViewById(R.id.name);
        address = itemView.findViewById(R.id.addr);
        img = itemView.findViewById(R.id.image);
    }
}}

MainActivity.java

public class MainActivity extends AppCompatActivity {
TextView txtChage;
Button btnChage, RemoveRecylerView;
RecyclerView recyclerView;
ArrayList<DataList> arrnames = new ArrayList<>();

ArrayList<DataList> arrData = new ArrayList<>();

dbHelper dbhelper;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    txtChage = findViewById(R.id.txt);
    btnChage = findViewById(R.id.ShowRecylerView);
    recyclerView = findViewById(R.id.recyclerView);
    RemoveRecylerView = findViewById(R.id.RemoveRecylerView);


    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    RecyclerViewAdapter adapter = new RecyclerViewAdapter(this, arrnames);
    recyclerView.setAdapter(adapter);

    dbhelper = dbHelper.getDb(this);
    if (!dbhelper.CheckDb()) {
        dbhelper.CreateDb(this);
    }
    dbhelper.openDb();
    arrData = dbhelper.getData();

    for (int i = 0; i < arrData.size(); i++) {
        String name = arrData.get(i).getName();
        String address = arrData.get(i).getAddress();
        byte[] img = arrData.get(i).getImage();

        arrnames.add(new DataList(name, address, img));
        adapter.notifyDataSetChanged();
    }


    adapter.notifyDataSetChanged();

    btnChage.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            txtChage.setText("Recycler Visible");
            recyclerView.setVisibility(View.VISIBLE);
            btnChage.setVisibility(View.GONE);
            RemoveRecylerView.setVisibility(View.VISIBLE);
        }
    });
    RemoveRecylerView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            txtChage.setText("Recycler Gone");
            recyclerView.setVisibility(View.GONE);
            btnChage.setVisibility(View.VISIBLE);
            RemoveRecylerView.setVisibility(View.GONE);

        }
    });
}

dbHelper.java

public class dbHelper extends SQLiteOpenHelper {
private static String Dbname = "StudentInfo.sqlite";
private String DbPath;
private SQLiteDatabase mainDb;

public dbHelper(Context context) {
    super(context, Dbname, null, 1);
    DbPath = "/data/data/" + context.getPackageName() + "/databases";
}

public static synchronized dbHelper getDb(Context context) {
    return new dbHelper(context);
}

public boolean CheckDb() {
    SQLiteDatabase database;
    try {
        database = SQLiteDatabase.openDatabase(DbPath + "/" + Dbname, null, SQLiteDatabase.OPEN_READONLY);
    } catch (Exception e) {
        database = null;
    }
    return database != null;
}

public void CreateDb(Context context) {
    this.getReadableDatabase();
    this.close();

    try {


        InputStream inputStream = context.getAssets().open(Dbname);

        String filepath = DbPath + "/" + Dbname;

        FileOutputStream fos = new FileOutputStream(filepath);

        byte[] bytes = new byte[1024];

        int length;

        while ((length = inputStream.read(bytes)) > 0) {
            fos.write(bytes, 0, length);
        }

        fos.close();
        fos.flush();
        inputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public ArrayList<DataList> getData() {
    Cursor cursor = mainDb.rawQuery("Select * from Profile", null);
    ArrayList<DataList> arrData = new ArrayList<>();

    while (cursor.moveToNext()) {
        String name = cursor.getString(1);
        String Address = cursor.getString(2);
        byte[] img = cursor.getBlob(3);
        arrData.add(new DataList(name, Address, img));

    }
    return arrData;
}

public void openDb() {
    mainDb = SQLiteDatabase.openDatabase(DbPath + "/" + Dbname, null, SQLiteDatabase.OPEN_READWRITE);
}


@Override
public void onCreate(SQLiteDatabase db) {


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}}

DataList.java

public class DataList {

private int id;
private byte[] image;
private String name;
private String Address;

public DataList(String name, String Address, byte[] image) {

    this.name = name;
    this.Address = Address;
    this.image = image;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public byte[] getImage() {
    return image;
}

public void setImage(byte[] image) {
    this.image = image;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAddress() {
    return Address;
}

public void setAddress(String Address) {
    this.Address = Address;
}

}

Upvotes: 0

Related Questions