Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading Lots Of Images From Server In Android

Edit 2: Even after the bounty, no one could give a full solution. So I solved it myself. I have posted my solution below.

I searched about it on Google and also found various good results. But since I'm not expert in server and backend kind of terms, I would like to ask it in a separate question. I have a website hosting which helps me run an institution's website.

  1. In my android app, there should be an option such as "Photo Gallery" which shows images from my website (where I upload images in a folder). I think it can be done by managing some JSON files.

  2. Let there be a folder \public_html\MyApp\Images which consists of folders like "Event1", "Event2", and so on. I upload images to these different folders and my Android app should load them (maybe separately giving users a choice to pick a folder).

Is it possible? If any advanced database system is required, please give me some intro links for that. Is there any way a JSON is automatically generated containing the links to images?

Edit: I had recently bought website hosting for backend kind of things and I haven't done any server-side scripting or anything. That's why a detailed answer is required. I don't know how to manage those images, simply upload images and put links in JSON or use some MySQL, SQL or whatever terms which I've never used.

like image 927
Paras Sidhu Avatar asked Dec 07 '22 17:12

Paras Sidhu


2 Answers

By the looks of it, you need 2 new database tables. One to store your albums, the other to store the individual images.

CREATE TABLE `album` (
   `album_id` INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
   `album_name` VARCHAR(255) NOT NULL,
   `album_date` DATE NOT NULL);

album would store all the album metadata so it would store Event1 with its details, Event2 and so on..

CREATE TABLE `images` (
   `image_id` INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,
   `album_id` INT(11) NOT NULL,
   `image_date` DATE NOT NULL,
   `image_path` VARCHAR(255) NOT NULL,
FOREIGN KEY (album_id) REFERENCES album(album_id));

images would store all the images with their paths and the albums which they belong to using the album_id as the foreign key

Your queries for all images would be

SELECT * FROM `images`;

Images from a particular album would be queried like

SELECT * FROM `images` WHERE `album_id` = *YOUR_ID*;

The output from the SQL query can be JSON encoded using

json_encode($query_result); //PHP code

EDIT: Gallery App Example On Android Hive

It seems like you're saving images as BLOB directly into the database hence it's giving you the 64kb limit. It is better if you save paths of the images. If that is not possible then base64 encode the images and store them in a column of type VARCHAR.

like image 64
Tanushree C Avatar answered Dec 10 '22 11:12

Tanushree C


This solution does not need database.

You need two API endpoints in PHP

Upload images /api/upload

<?php

$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
// Check if file already exists
if (file_exists($target_file)) {
    echo "Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
    echo "Sorry, your file is too large.";
    $uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
    $uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "Sorry, there was an error uploading your file.";
    }
}
?>
  1. List images

First list all directories under the upload directory:

$array = glob("uploads/*", GLOB_ONLYDIR);
echo json_encode($array);
  1. If you want a specific folder, you can do so by sending the folder name and append to request URL and list all files.

    $array = glob("uploads/".$_REQUEST['dir']."/*"); echo json_encode($array);

EDIT

// provides the current working directory
echo getcwd() ."\r\n";
// then navigate to path
// glob(getcwd() . DIRECTORY_OF_IMAGES));
// GLOB_BRACE tells to find all extensions in the brace
$array = glob(getcwd() . "/web/bundles/img/*.{jpg,gif,png}", GLOB_BRACE);

Read this article http://php.net/manual/en/function.glob.php

like image 43
Gaurav Avatar answered Dec 10 '22 13:12

Gaurav