Android - 在GridView中使用PHP显示服务器文件夹中的所有图像

时间:2015-06-04 13:19:58

标签: php android android-service android-camera android-imageview

我正在尝试使用相机捕获图像或录制视频,然后上传到我的服务器。在服务器端,我使用PHP语言读取文件并将其移动到特定位置。现在我想显示存储在我服务器上的所有这些图像。请帮帮我。

这是上传图片PHP脚本

<?php
 
// Path to move uploaded files
$target_path = "uploads/";
 
// array for final json respone
$response = array();
 
// getting server ip address
$server_ip = gethostbyname(gethostname());
 
// final file url that is being uploaded
$file_upload_url = 'http://' . $server_ip . '/' . 'AndroidFileUpload' . '/' . $target_path;
 
 
if (isset($_FILES['image']['name'])) {
    $target_path = $target_path . basename($_FILES['image']['name']);
 
    // reading other post parameters
    $email = isset($_POST['email']) ? $_POST['email'] : '';
    $website = isset($_POST['website']) ? $_POST['website'] : '';
 
    $response['file_name'] = basename($_FILES['image']['name']);
    $response['email'] = $email;
    $response['website'] = $website;
 
    try {
        // Throws exception incase file is not being moved
        if (!move_uploaded_file($_FILES['image']['tmp_name'], $target_path)) {
            // make error flag true
            $response['error'] = true;
            $response['message'] = 'Could not move the file!';
        }
 
        // File successfully uploaded
        $response['message'] = 'File uploaded successfully!';
        $response['error'] = false;
        $response['file_path'] = $file_upload_url . basename($_FILES['image']['name']);
    } catch (Exception $e) {
        // Exception occurred. Make error flag true
        $response['error'] = true;
        $response['message'] = $e->getMessage();
    }
} else {
    // File parameter is missing
    $response['error'] = true;
    $response['message'] = 'Not received any file!F';
}
 
// Echo final json response to client
echo json_encode($response);
?>

上传相机图片:

enter image description here

enter image description here

我希望在我再次上传图片时同步显示这些图像。

Config.java

public class Config {
// File upload url (replace the ip with your server address)
public static final String FILE_UPLOAD_URL = "http://wangjian.site90.net/AndroidFileUpload/fileUpload.php";
// Directory name to store captured images and videos
public static final String IMAGE_DIRECTORY_NAME = "Android File Upload";

我是这个东西的新手所以任何帮助将不胜感激。非常感谢!如果需要,我会上传更多详细信息。

1 个答案:

答案 0 :(得分:2)

让API端点返回您上传的图片的网址,然后从应用中调用它们。

像,

public static final String FILE_DOWNLOAD_URL = "http://wangjian.site90.net/AndroidFileUpload/getUserPhotos.php";

让它返回一些JSON数组,如

{
   "urls" : [
     {   
        "url" : "url of pic 1"
     },
     {
        "url" : "url of pic 2"
     },
     ..
    ]
}

拥有自定义GridAdpater,其中包含ImageView。使用Picasso等库可以使用自定义视图(GridView)自定义适配器将图片从网址加载到ImageView

每当用户在屏幕上时调用此API端点,这样您就可以获取上传的照片列表并每次显示它们。