title: Fetch image from mysql database to android by image path
body: <p>Im currently creating an android app which includes the image gallery. But im having a hard time when trying to fetch images from my database. I save the path of my image and trying to save it to json then parse it to my android.
  Here's my code : </p>
  
  <p>photo.php</p>
  
  <p>photo.php</p>
  
  <pre><code>  &lt;?php
  
  define('HOST','');
  define('USER','');
  define('PASS','');
  define('DB','');
  $con = mysqli_connect(HOST,USER,PASS,DB);
  
  // query the application data
  $sql = "SELECT image FROM photos";
  $result = mysqli_query($con, $sql);
  
  // an array to save the application data
  $rows = array();
  
  // iterate to query result and add every rows into array
  while($row = mysqli_fetch_array($result)) {
  $rows[]=$row;
  
  }
  
  // close the database connection
  mysqli_close($con);
  
  // echo the application data in json format
  echo json_encode($rows);
  
  ?&gt;
  </code></pre>
  
  <p>FetchImage.java</p>
  
  <pre><code> public class FetchImage extends Activity {
  Button load_img;
  ImageView img;
  Bitmap bitmap;
  ProgressDialog pDialog;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_fecth_image);
      load_img = (Button)findViewById(R.id.load);
      img = (ImageView)findViewById(R.id.img);
      load_img.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View arg0) {
              // TODO Auto-generated method stub
               new LoadImage().execute("http://iguideph-001-site1.btempurl.com/PhotoUpload/uploads/vigan1.jpg");
          }
      });
  
  
  }
  private class LoadImage extends AsyncTask&lt;String, String, Bitmap&gt; {
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          //pDialog = new ProgressDialog(FetchImage.this);
         // pDialog.setMessage("Loading Image ....");
         // pDialog.show();
  
      }
       protected Bitmap doInBackground(String... args) {
           try {
                 bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());
  
          } catch (Exception e) {
                e.printStackTrace();
          }
          return bitmap;
       }
  
       protected void onPostExecute(Bitmap image) {
  
           if(image != null){
           img.setImageBitmap(image);
           pDialog.dismiss();
  
           }else{
  
           pDialog.dismiss();
           Toast.makeText(FetchImage.this, "Image Does Not exist or Network Error", Toast.LENGTH_SHORT).show();
  
           }
       }
   }
  
    }
  </code></pre>
  
  <p>activity_fetch_image.xml</p>
  
  <pre><code> &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  tools:context=".MainActivity" &gt;
  
  &lt;Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:id="@+id/load"
      android:text="Load Image" /&gt;
  
  &lt;ImageView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:id="@+id/img"/&gt;
  
    &lt;/LinearLayout&gt;
  </code></pre>