Android將圖片上傳到php服務(wù)器的實(shí)例代碼
layout中很普通,就是兩個(gè)button和一個(gè)ImageView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/test"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:text="button1"
android:textAllCaps="false" />
<Button
android:id="@+id/test2"
android:layout_width="368dp"
android:layout_height="wrap_content"
android:text="button2"
android:textAllCaps="false"
/>
<ImageView
android:id="@+id/image"
android:layout_width="0dp"
android:layout_height="495dp"
/>
</LinearLayout>
在主頁(yè)面中給按鈕添加事件:
package success.xiaoyu.okhttp3;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
private Button button1,button2;
private ImageView imageView;
private Handler handler = new Handler(){
public void handleMessage(Message msg) {
Bitmap bitmap = (Bitmap)msg.obj;
imageView.setImageBitmap(bitmap);
//Toast.makeText(MainActivity.this, Environment.getExternalStorageDirectory()+"",Toast.LENGTH_LONG).show();
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
button1 = (Button)findViewById(R.id.test);
button2 = (Button)findViewById(R.id.test2);
imageView = (ImageView)findViewById(R.id.image);
button1.setOnClickListener(new View.OnClickListener() {//將服務(wù)器的圖片讀取到本地
public void onClick(View view) {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url("http://115.159.217.226/xy.png")
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
public void onFailure(Call call, IOException e) {
}
public void onResponse(Call call, Response response) throws IOException {
InputStream inputStream = response.body().byteStream();
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Message msg = new Message();
msg.obj = bitmap;
handler.sendMessage(msg);
}
});
}
});
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
uploadMultiFile();
}
});
}
private void uploadMultiFile() {//將圖片發(fā)送到服務(wù)器
final String url = "http://115.159.217.226/upload.php";
File file = new File( Environment.getExternalStorageDirectory()+"/storage/emulated/0/", "xy.jpg");
RequestBody fileBody = RequestBody.create(MediaType.parse("application/octet-stream"), file);
File file2 = new File( Environment.getExternalStorageDirectory()+"/storage/emulated/0/", "yyw.jpg");
RequestBody fileBody2 = RequestBody.create(MediaType.parse("application/octet-stream"), file2);
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("image1", "xy.jpg", fileBody)
.addFormDataPart("image2", "yyw.jpg", fileBody2)
.build();
Request request = new Request.Builder()
.url(url)
.post(requestBody)
.build();
final okhttp3.OkHttpClient.Builder httpBuilder = new OkHttpClient.Builder();
OkHttpClient okHttpClient = httpBuilder
//設(shè)置超時(shí)
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(15, TimeUnit.SECONDS)
.build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e("aa", "uploadMultiFile() e=" + e);
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.i("bb", "uploadMultiFile() response=" + response.body().string());
}
});
}
}
服務(wù)器端代碼:
<?php
header('Content-type: application/json;charset=utf-8');
if(empty($_FILES)) die('{"status":0,"msg":"錯(cuò)誤提交"}');
$dirPath = './img/';//設(shè)置文件保存的目錄
if(!is_dir($dirPath)){
//目錄不存在則創(chuàng)建目錄
@mkdir($dirPath);
}
$count = count($_FILES);//所有文件數(shù)
if($count<1) die('{"status":0,"msg":"錯(cuò)誤提交"}');//沒(méi)有提交的文件
$success = $failure = 0;
foreach($_FILES as $key => $value){
//循環(huán)遍歷數(shù)據(jù)
$tmp = $value['name'];//獲取上傳文件名
$tmpName = $value['tmp_name'];//臨時(shí)文件路徑
//上傳的文件會(huì)被保存到php臨時(shí)目錄,調(diào)用函數(shù)將文件復(fù)制到指定目錄
if(move_uploaded_file($tmpName,$dirPath.date('YmdHis').'_'.$tmp)){
$success++;
}else{
$failure++;
}
}
$arr['status'] = 1;
$arr['msg'] = '提交成功';
$arr['success'] = $success;
$arr['failure'] = $failure;
echo json_encode($arr);
?>
總結(jié)
以上所述是小編給大家介紹的Android將圖片上傳到php服務(wù)器的實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android讀取服務(wù)器圖片的三種方法
- Android 通過(guò)Base64上傳圖片到服務(wù)器實(shí)現(xiàn)實(shí)例
- Android 通過(guò)webservice上傳多張圖片到指定服務(wù)器詳解
- Android選擇圖片或拍照?qǐng)D片上傳到服務(wù)器
- Android開(kāi)發(fā)中調(diào)用系統(tǒng)相冊(cè)上傳圖片到服務(wù)器OPPO等部分手機(jī)上出現(xiàn)短暫的顯示桌面問(wèn)題的解決方法
- Android Socket服務(wù)端與客戶端用字符串的方式互相傳遞圖片的方法
- Android使用post方式上傳圖片到服務(wù)器的方法
- Android異步上傳圖片到PHP服務(wù)器
- Android從服務(wù)器獲取圖片的實(shí)例方法
- android傳送照片到FTP服務(wù)器的實(shí)現(xiàn)代碼
相關(guān)文章
Android獲取應(yīng)用程序大小和緩存的實(shí)例代碼
這篇文章主要介紹了Android獲取應(yīng)用程序大小和緩存的實(shí)例代碼的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-10-10
Android實(shí)現(xiàn)判斷某個(gè)服務(wù)是否正在運(yùn)行的方法
這篇文章主要介紹了Android實(shí)現(xiàn)判斷某個(gè)服務(wù)是否正在運(yùn)行的方法,涉及Android針對(duì)系統(tǒng)服務(wù)運(yùn)行狀態(tài)的判斷技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android實(shí)用的代碼片段 常用代碼總結(jié)
這篇文章主要介紹了Android實(shí)用的代碼片段 常用代碼總結(jié),需要的朋友可以參考下2014-09-09
Android之用PopupWindow實(shí)現(xiàn)彈出菜單的方法詳解
本篇文章是對(duì)在Android中,用PopupWindow實(shí)現(xiàn)彈出菜單的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android Camera開(kāi)發(fā)手電筒功能
這篇文章主要介紹了Android Camera開(kāi)發(fā)手電筒功能的相關(guān)資料,需要的朋友可以參考下2016-07-07
Android Flutter實(shí)現(xiàn)GIF動(dòng)畫(huà)效果的方法詳解
如果我們想對(duì)某個(gè)組件實(shí)現(xiàn)一組動(dòng)效應(yīng)該怎么辦呢?本文將利用Android Flutter實(shí)現(xiàn)GIF動(dòng)畫(huà)效果,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-06-06
Android編程之界面實(shí)現(xiàn)全屏顯示的方法(2種方法)
這篇文章主要介紹了Android編程之界面實(shí)現(xiàn)全屏顯示的方法,結(jié)合實(shí)例分析了Java代碼中設(shè)置與Manifest文件設(shè)置2種實(shí)現(xiàn)方法,需要的朋友可以參考下2016-01-01

