2

I am going to create an activity feed like this:

Activity Feed

and I wonder, how will I retrieve the image and how to store it in my MySQL database. Help me out!

Ben Morris
  • 606
  • 5
  • 24
IamAndroida
  • 75
  • 2
  • 3
  • 11
  • 3
    You have to save only image path in database and retrieve image using that path – Angel Jan 31 '14 at 05:19
  • Yes it is possible.. You can save it as byte[] array or you can save image path or image name in your database and access it.. – Piyush Jan 31 '14 at 05:42
  • How can i access it if i use php? i have saved an image in my cpanel (it is a host/domain) and i paid for it. – IamAndroida Jan 31 '14 at 05:56
  • @IamAndroida: Refer link given in my answer you should get idea how to deal php with android .. – Chirag Ghori Jan 31 '14 at 06:38
  • @Angel i am retrieving using image path itself but i am getting the dummy image file which is default in android emulator but i cant find my image in emulator while compiling – Sandeep V Apr 11 '14 at 12:32
  • i have posted the code what @Angel pls check on it where i must correct http://stackoverflow.com/questions/23010339/i-am-retrieving-an-image-from-mysql-using-json-but-getting-error-in-android?noredirect=1#comment35147944_23010339 – Sandeep V Apr 11 '14 at 12:33

2 Answers2

2
  1. Download the Image
  2. Save it as string in database

Here is how to do it

  1. To download the image use the Universal Image Loader

    ImageLoader imageLoader = ImageLoader.getInstance();
    imageLoader.init(ImageLoaderConfiguration.createDefault(context));
    OR
    ImageLoaderConfiguration config = new
        ImageLoaderConfiguration.Builder(context)
        .maxImageWidthForMemoryCache(800)
        .maxImageHeightForMemoryCache(480)
        .httpConnectTimeout(5000)
        .httpReadTimeout(20000)
        .threadPoolSize(5)
        .threadPriority(Thread.MIN_PRIORITY + 3)
        .denyCacheImageMultipleSizesInMemory()
        .build();
    imageLoader.init(config);
    options = new DisplayImageOptions.Builder()
        .showStubImage(R.drawable.loading)
        .cacheInMemory()
        .cacheOnDisc()
        .build();
    imageLoader.displayImage(ProductsImageURL,imagView,options, new ImageLoadingListener(){});
    

In this ImageLoadingListener you get a method that is called when loading is done and save the image as string

or make any http connection to download the image as here How to download and save an image in Android

  1. Once the image is downloaded convert it into string and store it in database

    public Bitmap StringToBitMap(String encodedString) {
        try {
            byte[] encodeByte = Base64.decode(encodedString, Base64.DEFAULT);
            Bitmap bitmap = BitmapFactory.decodeByteArray(encodeByte, 0,
                    encodeByte.length);
            return bitmap;
        } catch (Exception e) {
            e.getMessage();
            return null;
        }
    }
    
    public String BitMapToString(Bitmap bitmap) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] b = baos.toByteArray();
        String temp = Base64.encodeToString(b, Base64.DEFAULT);
        return temp;
    }
    
Community
  • 1
  • 1
Girish Nair
  • 5,148
  • 5
  • 40
  • 61
0

Here are the general steps if you want to store the image itself into your database.

i. Retrieve the image using its URL and keep it in memory as a byte[]. There are several ways to do this, including creating a ByteArrayBuffer and appending bytes to its end. You can then retrieve the final byte[] with ByteArrayBuffer.toByteArray().

ii. You need to create a table with a column of type BLOB, e.g.

CREATE TABLE image (id INTEGER PRIMARY KEY AUTOINCREMENT, url TEXT, data BLOB)

iii. You would then insert the image byte[] as you would any other data type, for example by create a ContentValues and then passing it to SQLiteDatabase.insert().

iv. Retrieve the byte[] from SQLite and build an image. Use Cursor.getBlob() to grab the byte[] and then do:

ByteArrayInputStream byteArrayImageStream = new ByteArrayImageStream(yourByteArray);
Bitmap yourImage = BitmapFactory.decodeStream(byteArrayImageStream);
stackoverflowuser2010
  • 38,621
  • 48
  • 169
  • 217
  • Is this applicable using a hosted database? im not using SQLitedtabase, im using mysql database and i also use php as bridge between android and mysql – IamAndroida Jan 31 '14 at 05:40
  • That is a completely different scenario, however it seems perfectly doable. Here is the first link I found when I searched for "php download image to mysql": http://www.phpriot.com/articles/images-in-mysql Once you save the image into MySQL, you just need to have your Android app call into PHP and get the image out. – stackoverflowuser2010 Jan 31 '14 at 06:22