【问题标题】:Android - Display all images from server folder using PHP in GridViewAndroid - 在 GridView 中使用 PHP 显示服务器文件夹中的所有图像
【发布时间】:2015-08-19 03:05:00
【问题描述】:

我正在尝试使用相机拍摄图像或录制视频,然后上传到我的服务器。在服务器端,我使用 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);
?>

上传相机图片:

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

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";

我是这方面的新手,因此我们将不胜感激。非常感谢!如果需要,我会上传更多详细信息。

【问题讨论】:

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


    【解决方案1】:

    让 API 端点返回您上传的图像的 URL,然后从应用程序调用它们。

    喜欢,

    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 之类的库将图像从 url 加载到您的 GridView 中,使用带有自定义视图的自定义适配器(此处为 ImageView)。

    每当用户在屏幕上时调用此 API 端点,以便您能够获取已上传照片的列表并每次都显示它们。

    【讨论】:

    • 是的,伙计,我知道使用 json 来转换数据,但是通过管理这个应用程序通过一系列复杂的方法,我想更容易,比如当用户上传照片时,图像上传到 imageview 是自动的.无论如何谢谢兄弟
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多