Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
image stored in sqlite database but its not retrieve in CircleimageView

What I have tried:

1. DatabaseHelper.class

public void insertImage(byte[] imageBytes) {

       SQLiteDatabase db = this.getReadableDatabase();
       ContentValues cv = new ContentValues();
       cv.put(IMAGE, imageBytes);
       db.insert(TABLE_NAME, null, cv);
   }

   public byte[] retreiveImageFromDB() {

       SQLiteDatabase db = this.getReadableDatabase();

       Cursor cur = db.query(true, TABLE_NAME, new String[]{IMAGE},
               null, null, null, null,
               COL_1 + " DESC", "1" );

       if (cur.moveToFirst()) {
           byte[] blob = cur.getBlob(cur.getColumnIndex("IMAGE"));
           cur.close();
           return blob;
       }
       cur.close();
       return null;
   }


2.Utils.class

public static byte[] getImageBytes(Bitmap bitmap) {
       ByteArrayOutputStream stream = new ByteArrayOutputStream();
       bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
       return stream.toByteArray();
   }

   public static Bitmap getImage(byte[] image) {
       return BitmapFactory.decodeByteArray(image, 0, image.length);
   }

   public static byte[] getBytes(InputStream inputStream) throws IOException {
       ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
       int bufferSize = 1024;
       byte[] buffer = new byte[bufferSize];

       int len = 0;
       while ((len = inputStream.read(buffer)) != -1) {
           byteBuffer.write(buffer, 0, len);
       }
       return byteBuffer.toByteArray();
   }


3.ProfileFragment.class


<pre>Boolean saveImageInDB(Uri selectedImageUri) {

        try {
            //databaseHelper.onOpen(null);
            InputStream iStream = getActivity().getContentResolver().openInputStream(selectedImageUri);
            byte[] inputData = Utils.getBytes(iStream);
            databaseHelper.insertImage(inputData);
            //databaseHelper.close();
            return true;
        } catch (IOException ioe) {
            Log.e(TAG, "<saveImageInDB> Error : " + ioe.getLocalizedMessage());
            //databaseHelper.close();
            return false;
        }
    }

    Boolean loadImageFromDB() {
        try {
            //databaseHelper.onOpen(null);
            byte[] bytes = databaseHelper.retreiveImageFromDB();
            //databaseHelper.close();
            // Show Image from DB in ImageView
            circleView.setImageBitmap(Utils.getImage(bytes));
            return true;
        } catch (Exception e) {
            Log.e(TAG, "<loadImageFromDB> Error : " + e.getLocalizedMessage());
           //databaseHelper.close();
            return false;
        }
    }
Posted
Comments
David Crow 31-Jan-18 9:31am    
What exactly is the problem? You've just showed a bunch of code with no supporting text detailing what the problem is, what you've tried, or what is (not) being shown. Are any exceptions being thrown? Have you stepped through the code using the debugger?
ZurdoDev 31-Jan-18 10:51am    
Why doesn't it work? Debug your code.
Android_Virus 1-Feb-18 2:19am    
Problem is when i move on HomeFragment.class and return come to the ProfileFragment.class image was automatically removed @David Crow
David Crow 1-Feb-18 10:49am    
"Problem is when i move on HomeFragment.class..."

What exactly does "move on" mean in the context of Java code?

"...image was automatically removed"

From where?
Android_Virus 2-Feb-18 1:41am    
Image removed from ImageView

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900