Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将视频和图像从Android上传到PHP服务器?_Php_Android - Fatal编程技术网

将视频和图像从Android上传到PHP服务器?

将视频和图像从Android上传到PHP服务器?,php,android,Php,Android,我想将媒体文件从Android设备存储到PHP服务器,比如视频和图像 任何链接或教程。 我想在服务器端保存,怎么做???? 在Android端,如何检查媒体文件大小限制、视频缩略图等?首先尝试一下 Android API有一组函数,允许您使用HTTP请求、POST、GET等。在本教程中,我们将制作一个应用程序,允许您使用POST请求更新服务器中文件的内容 服务器端代码 我们的服务器端代码将非常简单,它将用PHP编写。代码将从post请求中获取数据,用数据更新文件并加载此文件以在浏览器中显示 制作

我想将媒体文件从Android设备存储到PHP服务器,比如视频和图像

任何链接或教程。 我想在服务器端保存,怎么做???? 在Android端,如何检查媒体文件大小限制、视频缩略图等?

首先尝试一下

Android API有一组函数,允许您使用HTTP请求、POST、GET等。在本教程中,我们将制作一个应用程序,允许您使用POST请求更新服务器中文件的内容

服务器端代码

我们的服务器端代码将非常简单,它将用PHP编写。代码将从post请求中获取数据,用数据更新文件并加载此文件以在浏览器中显示

制作一个包含以下内容的文件,并将其上载到服务器。如果你知道这个文件的网址,我们会把它交给我们的Android应用程序,让它知道在哪里发布数据

<?php
// get the "message" variable from the post request
// this is the data coming from the Android app
$message=$_POST["message"]; 
// specify the file where we will save the contents of the variable message
$filename="androidmessages.html";
// write (append) the data to the file
file_put_contents($filename,$message."<br />",FILE_APPEND);
// load the contents of the file to a variable
$androidmessages=file_get_contents($filename);
// display the contents of the variable (which has the contents of the file)
echo $androidmessages;
?>

应用程序端代码

在应用程序端,我们的界面只不过是一个文本框和一个按钮。按钮链接到我们活动中的send()函数,当用户按下按钮时,函数send()将被执行

layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
        android:text="Message"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        /> 

    <EditText
        android:id="@+id/msgTextField"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <Button
        android:text="Send"
        android:id="@+id/sendButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="send"
        /> 

</LinearLayout>

我们只需要一个活动,send()函数将发挥所有作用。发送功能中的步骤包括:

从文本框中获取内容并将其存储在变量中 向php脚本发出http post请求 清除文本框

HelloWorldActivity.java
package com.yoursite.helloworld;

import java.io.IOException;
import org.apache.http.client.ClientProtocolException;


import java.util.ArrayList;
import java.util.List;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;


import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;

// import everything you need
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class HelloWorldActivity extends Activity {

    Button sendButton;

    EditText msgTextField;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // load the layout
        setContentView(R.layout.main);        

        // make message text field object
        msgTextField = (EditText) findViewById(R.id.msgTextField);
        // make send button object
        sendButton = (Button) findViewById(R.id.sendButton);

    }

    // this is the function that gets called when you click the button
    public void send(View v)
    {
        // get the message from the message text box
        String msg = msgTextField.getText().toString();  

        // make sure the fields are not empty
        if (msg.length()>0)
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://yourwebsite.com/yourPhpScript.php");
         try {
           List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
           nameValuePairs.add(new BasicNameValuePair("id", "12345"));
           nameValuePairs.add(new BasicNameValuePair("message", msg));
           httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
           httpclient.execute(httppost);
           msgTextField.setText(""); // clear text box
         } catch (ClientProtocolException e) {
             // TODO Auto-generated catch block
         } catch (IOException e) {
             // TODO Auto-generated catch block
         }

        }
        else
        {
            // display message if text fields are empty
            Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
        }

    }

}
HelloWorldActivity.java
包com.yoursite.helloworld;
导入java.io.IOException;
导入org.apache.http.client.ClientProtocolException;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.message.BasicNameValuePair;
//导入您需要的所有内容
导入android.app.Activity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.Toast;
公共类HelloWorldActivity扩展了活动{
按钮发送按钮;
编辑文本msgTextField;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//加载布局
setContentView(R.layout.main);
//使消息文本字段成为对象
msgTextField=(EditText)findViewById(R.id.msgTextField);
//生成发送按钮对象
sendButton=(按钮)findViewById(R.id.sendButton);
}
//这是单击按钮时调用的函数
公共无效发送(视图五)
{
//从消息文本框中获取消息
字符串msg=msgTextField.getText().toString();
//确保字段不是空的
如果(msg.length()>0)
{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://yourwebsite.com/yourPhpScript.php");
试一试{
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“id”,“12345”);
添加(新的BasicNameValuePair(“消息”,msg));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
httpclient.execute(httppost);
msgTextField.setText(“”;//清除文本框
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
}
其他的
{
//如果文本字段为空,则显示消息
Toast.makeText(getBaseContext(),“所有字段都是必需的”,Toast.LENGTH_SHORT.show();
}
}
}
这是清单文件

manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.yoursite.helloworld"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".HelloWorldActivity"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>

你应该会看到你发送的信息。发送另一条消息,刷新网站以查看新消息。

要开始,请尝试此操作

Android API有一组函数,允许您使用HTTP请求、POST、GET等。在本教程中,我们将制作一个应用程序,允许您使用POST请求更新服务器中文件的内容

服务器端代码

我们的服务器端代码将非常简单,它将用PHP编写。代码将从post请求中获取数据,用数据更新文件并加载此文件以在浏览器中显示

制作一个包含以下内容的文件,并将其上载到服务器。如果你知道这个文件的网址,我们会把它交给我们的Android应用程序,让它知道在哪里发布数据

<?php
// get the "message" variable from the post request
// this is the data coming from the Android app
$message=$_POST["message"]; 
// specify the file where we will save the contents of the variable message
$filename="androidmessages.html";
// write (append) the data to the file
file_put_contents($filename,$message."<br />",FILE_APPEND);
// load the contents of the file to a variable
$androidmessages=file_get_contents($filename);
// display the contents of the variable (which has the contents of the file)
echo $androidmessages;
?>

应用程序端代码

在应用程序端,我们的界面只不过是一个文本框和一个按钮。按钮链接到我们活动中的send()函数,当用户按下按钮时,函数send()将被执行

layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <TextView
        android:text="Message"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        /> 

    <EditText
        android:id="@+id/msgTextField"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
    <Button
        android:text="Send"
        android:id="@+id/sendButton"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="send"
        /> 

</LinearLayout>

我们只需要一个活动,send()函数将发挥所有作用。发送功能中的步骤包括:

从文本框中获取内容并将其存储在变量中 向php脚本发出http post请求 清除文本框

HelloWorldActivity.java
package com.yoursite.helloworld;

import java.io.IOException;
import org.apache.http.client.ClientProtocolException;


import java.util.ArrayList;
import java.util.List;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;


import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.message.BasicNameValuePair;

// import everything you need
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;


public class HelloWorldActivity extends Activity {

    Button sendButton;

    EditText msgTextField;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        // load the layout
        setContentView(R.layout.main);        

        // make message text field object
        msgTextField = (EditText) findViewById(R.id.msgTextField);
        // make send button object
        sendButton = (Button) findViewById(R.id.sendButton);

    }

    // this is the function that gets called when you click the button
    public void send(View v)
    {
        // get the message from the message text box
        String msg = msgTextField.getText().toString();  

        // make sure the fields are not empty
        if (msg.length()>0)
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://yourwebsite.com/yourPhpScript.php");
         try {
           List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
           nameValuePairs.add(new BasicNameValuePair("id", "12345"));
           nameValuePairs.add(new BasicNameValuePair("message", msg));
           httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
           httpclient.execute(httppost);
           msgTextField.setText(""); // clear text box
         } catch (ClientProtocolException e) {
             // TODO Auto-generated catch block
         } catch (IOException e) {
             // TODO Auto-generated catch block
         }

        }
        else
        {
            // display message if text fields are empty
            Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
        }

    }

}
HelloWorldActivity.java
包com.yoursite.helloworld;
导入java.io.IOException;
导入org.apache.http.client.ClientProtocolException;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.message.BasicNameValuePair;
//导入您需要的所有内容
导入android.app.Activity;
导入android.os.Bundle;
英普