1

I know there have been a lot of posts trying to get blobs from mysql into android but none of that did work for me (surely could be because of my average coding skills).

I have the following code (reduced to the necessary):

        protected String doInBackground(String... args) {
             List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("pid", pid));

             JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

            try {
                    products = json.getJSONArray(TAG_PRODUCTS);

                    for (int i = 0; i < products.length(); i++) {
                        JSONObject c = products.getJSONObject(i);

             ########### tried many things here ##########

                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }

which I got from a tutorial and modified it.

I tried a lot of things to get the blob to the imageview, for example

 try {
                          InputStream input = new ByteArrayInputStream(c.getString(TAG_PIC).getBytes("utf-8"));
                          Bitmap myBitmap = BitmapFactory.decodeStream(input);

                            ImageView imageview = (ImageView) findViewById(R.id.imageView);
                            imageview.setImageBitmap(myBitmap);
                        }   catch (Exception e) {
                        }

but nothing did work. Most people don't use "jParser.makeHttpRequest". I have the following php code:

 <?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);

$servername = "localhost";    $username = "root";    $password = "";  $dbname = "db";

$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

if (!$conn->set_charset("utf8")){
    exit();
} else {
}
if (isset($_GET["pid"])) {
    $pid = $_GET['pid'];

    $sql = "SELECT ID, Name, Bild FROM Produkt WHERE ID = $pid";
    $result = $conn->query($sql);

    if (!empty($result)) {
        $i = 0;
        if ($result->num_rows > 0) {
            while($row = $result->fetch_assoc()) {

               $product = array();
               $product['ID'] = $row['ID'];
               $product['Name'] = $row['Name'];
               $product['Bild'] = base64_encode($row['Bild']);

               $output['product'][$i] = $product;
            }
            $output['success'] = 1;
        } else {
            $output['success'] = 0 ;
        }
    }        
}
echo(json_encode($output));
$conn->close();
?>

I would be glad for help. Thanks in advance.

Ginbak
  • 105
  • 9

0 Answers0