2

In MySQL Database i have a table containing name and image. I want to get those details into android.

I used PHP to get the data from Database.

My PHP code:

<?php
$db_host = $_POST['db_host'];
$db_uname = $_POST['db_user_name'];
$db_password = $_POST['db_password'];
$conn = mysql_connect($db_host, $db_uname, $db_password);
$query = $_POST['query'];
$db_name = $_POST['db_name'];
mysql_select_db($db_name);
$result_set = mysql_query($query, $conn);
while($row = mysql_fetch_array($result_set)) {
    print "name:{$row['name']},image:" . base64_decode($row['image']) . ";
}
mysql_close($conn);

?>

I get name perfectly as it is and image in below format

LzlqLzRBQVFTa1pKUmdBQkFnRUFZQUJnQUFELzdnQU9RV1J2WW1VQVpBQUFBQUFCLytFVFhVVjRhV1lBQUUxTkFDb0FBQUFJQUFjQk1nQUNBQUFBRkFBQUFHSUJPd0FDQUFBQUJ3QUFBSFpIUmdBREFBQUFBUUFFQUFCSFNRQURBQUFBQVFBL0FBQ2NuUU

The format shown above is just as an example.

The string i get as image is saved to SQLite Database using ContentValues.

And now i need to display the image saved in SQLite on android screen.

My code is like :

 ByteArrayInputStream input_stream = new ByteArrayInputStream(moviesList.get(i).getMovieImage());
 Bitmap bitmap = BitmapFactory.decodeStream(input_stream);
 movie_image.setImageBitmap(bitmap);

But i am not able to get the image ?

Please help me to solve this issue. Thank You

3 Answers3

0

UPDATE

I'm not into Android development, but as far as the PHP part goes:

<?php 
// config.php

$db_host ='';
$db_uname = '';
$db_password = '';
$db_name = '';
$api_key = 'HERE_WE_DEFINE_A_SECRET_KEY';

?>

<?php
// file you're calling
require_once 'path/to/config.php';

$conn = mysql_connect($db_host, $db_uname, $db_password);

# check if your receiving a post call
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    if($_POST['api_key'] !== $api_key)
    {
        echo 'No access';
        exit(1);
    }

    switch($_POST['command'])
    {
        // this is how you call a certain action
        case 'GET_IMAGE':
            // Do the stuff you want to do.
            require_once 'path/to/getImage.php';
        break;
        default:
             // If no command is specified 
        break;
    }
}
?>

<?php
// getImage.php
if(!$conn)
{
    echo 'No connection';
    exit(1);
}
// sanitize input
if(!isset($_GET['id']) || !is_numeric($_GET['id']))
{
    echo 'Invalid ID';
    exit(1);
}
$image_id = $_GET['id'];
$query="SELECT * FROM images WHERE id = {$image_id}";

$result_set = mysql_query($query, $conn);
while($row = mysql_fetch_array($result_set)) 
{
    echo "name:{$row['name']},image:" . base64_decode($row['image']) ;
}
?>

Now if we call the url, we're in business - and we're safe!

yourhost.com/yourfile.php?api_key=HERE_WE_DEFINE_A_SECRET_KEY&command=GET_IMAGE&id=3


ORIGINAL POST Well in the php example above you have a parse error.

print "name:{$row['name']},image:" . base64_decode($row['image']) . ";

Needs an extra " before the semicolon.

print "name:{$row['name']},image:" . base64_decode($row['image']) . "";

PS: Did someone mention it's not really safe to send MYSQL credentials over HTTP? Even more, to _POST complete QUERIES?

1.) If your building an app, and someone uses a program like WIRESHARK you're in big trouble.

2.) You just shouldn't do this. Beter to set all credentials locally, have prepared QUERIES and use a secret passphrase (Basicly that's what an API key is) or something in order to retreive your data.

Lapidi
  • 116
  • 4
  • Yep. It was the mistake i made when i pasted the `code` to stack overflow. I got the data from `PHP` to `android` successfully. My question is Is the format i got in `PHP` is correct ? – Nimmagadda Gowtham Dec 17 '15 at 19:49
  • If sending the `input data (query)` through `POST` is not correct, can you please post some` code` which is better way to send the `input data to PHP.` – Nimmagadda Gowtham Dec 17 '15 at 19:56
  • I updated my post. But the real question of finding out your PHP generated data is correct, is by finding out what the input is for the `ByteArrayInputStream`, so the `BitmapFactory.decodeStream` can neatly decode it. I'm suspecting the – Lapidi Dec 17 '15 at 20:25
  • Hi `Lapidi`. Thank You for your explanation. I am getting the `image` as `String`. Is the format i get is correct. If that format is correct i can concentrate on changing code related to get image from `SQLite Database` – Nimmagadda Gowtham Dec 18 '15 at 18:06
0

And what about that?

byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
Hrabosch
  • 1,541
  • 8
  • 12
  • Hey your code worked. I have tried your code but `Base64.decode() method` accepts `byte[]` and `String` as first parameter. Using `string` solved this issue – Nimmagadda Gowtham Dec 18 '15 at 19:39
0

I'd recommend that you store the file on disc, create a file Uri and store the Uri instead of image data in the database.

If you still need to store the image data, have a look at this answer: https://stackoverflow.com/a/32646725/383676

Community
  • 1
  • 1
fejd
  • 2,545
  • 1
  • 16
  • 39
  • Hey I got solution to this. I did not save the image in `SQLite` but i saved `Base64 format` and then used `byte[] decodedImage = Base64.decode(base64format, Base64.DEFAULT);` `Bitmap bitmap = BitmapFactory.decodeByteArray(decodedImage, 0, decodedImage.length);` – Nimmagadda Gowtham Dec 18 '15 at 19:35