0

I store the images to SQLite by converting the bitmap to byte array.

Should I do the same thing? Getting the byte array from bitmap, then to JSON, then to PHP, and finally to MySQL.

If yes, how can I do that? I could store strings to MySQL from the app, but couldn't do it on byte arrays.

alecxe
  • 414,977
  • 106
  • 935
  • 1,083
Loua
  • 39
  • 1
  • 7
  • If you don't want to have process on images, simply save them on memory and store their reference in database. – Misagh Emamverdi Jun 02 '15 at 04:49
  • @MisaghEmamverdi It's not that I don't want, but as I said.. I don't know how. Could you please explain your way? and is it good if I have alot of images? – Loua Jun 02 '15 at 05:02
  • check this http://stackoverflow.com/questions/29342749/get-images-from-php-mysql-server-to-android-using-json – Tufan Jun 02 '15 at 05:20

6 Answers6

0

just convert the byte array into Base64 ... base64 technically is a string, so JSON it to your webservice, there convert it back from Base64 ... and the rest is history

here is a link for converting image to Base64 in android : How to convert a image into Base64 string?

btw, other guys are right it's not so efficient to store the image itself inthe db, storing a reference would be much better ... unless you do not want your images to be right on the sd card, which is something else, security wise !

Community
  • 1
  • 1
Muhammad Naderi
  • 2,450
  • 2
  • 20
  • 32
0

You can refer to this SO discussion that talks about uploading files to a server using Android.

As a matter of fact it is not recommended to store binary data into relational databases. Refer this SO Discussion. Rather a recommended way would be store the binary data on a server disk location as a file and simply place the path of the file within the database. This would prevent any corruption of data due to discrepancies in the database character set and encodings.

Community
  • 1
  • 1
Prahalad Deshpande
  • 3,993
  • 17
  • 20
0

As you are willing to use another solution that is more good, i am explaining you the below procedure. Instead of storing the image in the database, create a directory specifically for the images, after you successfully upload the image store the path of that image in your database. After that use that path to refer that image.

Sourabh Kumar Sharma
  • 2,756
  • 3
  • 20
  • 31
  • I followed [this tutorial](http://programmerguru.com/android-tutorial/how-to-upload-image-to-php-server/) to upload the image, but how can I retrieve it? or show it on the Android app? – Loua Jun 02 '15 at 08:41
  • are you storing the image in the database or are you storing the image in a folder? – Sourabh Kumar Sharma Jun 02 '15 at 08:50
  • Most answers recommended not to store in the database and store them in a folder instead, so I will leave the idea of storing the images in the database and try to store them in a folder instead. – Loua Jun 02 '15 at 08:56
  • yes that is good thing, so once the image is uploaded, store the path of the image in database, use that path as your image src in your android app. If you are searching a solution to show image from a web url, then i think this link explains that pretty well:http://stackoverflow.com/questions/2471935/how-to-load-an-imageview-by-url-in-android – Sourabh Kumar Sharma Jun 02 '15 at 08:59
  • I've been trying to do as [this answer](http://stackoverflow.com/a/9288544/4345044), there is no error, but nothing shows up.. even though [this answer](http://stackoverflow.com/a/23865531/4345044) works fine. What am i missing? – Loua Jun 02 '15 at 13:54
0

You can upload image via POST method, and then store reference in the database (ex:- images/img1.bmp)

Whenever you needed you can get file reference using http request(you need to code php for that request handling)

You can access image by using your servers public ip or domain for example : mydomain.com/app1/images/img1.bmp

This is just a one way to do it, so if you think about implementing look for file upload examples via POST

Hope this helps

nicole ino
  • 43
  • 9
  • I followed [this tutorial](http://programmerguru.com/android-tutorial/how-to-upload-image-to-php-server/) to upload the image, but how can I retrieve it? or show it on the Android app? – Loua Jun 02 '15 at 08:37
  • @Loua you get image URL from the database (ex:- "mydomain.com/app1/images/img1.bmp" so you can load it to your android app [Chek Chirag's Answer here](http://stackoverflow.com/questions/6407324/how-to-get-image-from-url-in-android) – nicole ino Jun 02 '15 at 10:16
  • you have to use exact URL when saving in database, so if you have domain name you put your images on `public_html` folder then you can acess your files via domain name like `mydomain.com/images/img1.jpg` if you dont have domain name you can use your ip address with username its like `101.101.111.1/~username/images/img1` this will work if you have linux hosting with cpanel- – nicole ino Jun 02 '15 at 10:21
  • I've been trying to do as [this answer](http://stackoverflow.com/a/9288544/4345044), there is no error, but nothing shows up.. even though [this answer](http://stackoverflow.com/a/23865531/4345044) works fine. What am i missing? – Loua Jun 02 '15 at 13:56
  • Try this code `try { InputStream is = (InputStream) new URL(url).getContent(); Drawable d = Drawable.createFromStream(is, "src name"); return d; } catch (Exception e) { return null; }` Dont froget to change URL to your image URL – nicole ino Jun 03 '15 at 12:38
0

Use Multipart file upload to send file to your web service (Use libraries like Android Asynchronous Http Client to make the job easy).Then you can encode the file in to Base64 and store in the database as text ,but it is not a best practice to store image files in database, you should save the image as file in the server and keep the path in MySql.

Y0Gi
  • 405
  • 4
  • 10
0
public static String imgToBase64(Bitmap bitmap) {
    ByteArrayOutputStream out = null;
    try {
        out = new ByteArrayOutputStream();
        // compress image
        bitmap.compress(Bitmap.CompressFormat.JPEG, 20, out);

        out.flush();
        out.close();

        byte[] imgBytes = out.toByteArray();
        return Base64.encodeToString(imgBytes, Base64.DEFAULT);
    } catch (Exception e) {
        return null;
    } finally {
        try {
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Thinsky
  • 4,090
  • 3
  • 11
  • 20