0

We are trying to upload image using the following code snippet:

       protected void onActivityResult(int requestCode, int resultcode, Intent intent) 
    {
            super.onActivityResult(requestCode, resultcode, intent);
      if (requestCode == 1)
     {
        if (intent != null && resultcode == RESULT_OK)
        {             

              Uri selectedImage = intent.getData();

              String[] filePathColumn = {MediaStore.Images.Media.DATA};
              Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);           
              cursor.moveToFirst();
              int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
              String filePath = cursor.getString(columnIndex);
              cursor.close();
              if(bmp != null && !bmp.isRecycled())
              {
                  bmp = null;               
              }

              bmp = BitmapFactory.decodeFile(filePath);
              iv.setBackgroundResource(0);
             iv.setImageBitmap(bmp);    

             File file = new File(filePath);
             try {
                fis = new FileInputStream(file);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
             try
             {
             Class.forName("com.mysql.jdbc.Driver");
             con=DriverManager.getConnection("jdbc:mysql://10.0.2.2/imgdb","root", "virus");
             PreparedStatement ps = 
                       con.prepareStatement("Update user_info set image=? WHERE ID=\""+id+"\"");

                ps.setBinaryStream(1,fis,(int)file.length());

                    ps.executeUpdate();
                    ps.close();
                    fis.close();
             }
             catch(Exception e)
             {
                 Toast.makeText(this,e.toString(),Toast.LENGTH_LONG).show();
             }

        }
        else
        {
            Log.d("Status:", "Photopicker canceled");           
        }

        }

After uploading in mysql DB we are getting [BLOB - 60.1KiB].. Whether image uploaded successfully? we are trying to retrieve it through the following code

 try {
     Class.forName("com.mysql.jdbc.Driver");
     con=DriverManager.getConnection("jdbc:mysql://10.0.2.2/imgdb","root","virus");
     Statement st = con.createStatement();
     String query = "Select image from user_info where ID=\""+id+"\"";


     PreparedStatement pstmt = con.prepareStatement(query);
     ResultSet rset = pstmt.executeQuery();
        rset.next();
        byte[] blob = rset.getBytes(1);//getBlob(1); 
     if(blob != null) {       
         byte[] decodedString = Base64.encode(blob, Base64.DEFAULT);      
         Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);   
         iv.setImageBitmap(decodedByte);      
         }  else {
             iv.setImageResource(R.drawable.chit_chat);         }

But we are unable to display in the image view.

Raman
  • 262
  • 1
  • 9
kdr
  • 207
  • 1
  • 7
  • 18

2 Answers2

1

It is not a very good idea to use a sql connection directly from android, you would be better served by a web service or a socket connection that is in charge of putting things into and back from Database.

Database connections are costly, and need to be more stable than httpconnections. There are not suited for mobile contexts. Here is a good thread about that.

Moreover, having a database close to your server will help you separate concerns. You will be able to test if bytes you get from DB are what you expect, on the server and use a simple asynctask to get the image on the android.

If you ant to persist with your solution, you should print out bytes received from database, why do you use base64 ?

Community
  • 1
  • 1
Snicolas
  • 37,840
  • 15
  • 114
  • 173
0

use decode in place of encode

byte[] decodedString = Base64.decode(blob, Base64.DEFAULT);
Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
Joy
  • 1