導航:首頁 > 編程語言 > 安卓開發圖片上傳代碼

安卓開發圖片上傳代碼

發布時間:2024-12-15 19:24:33

1. 安卓如何在後台上傳圖片等資料

public class EX08_11 extends Activity
{
/* 變數聲明
* newName:上傳後在伺服器上的文件名稱
* uploadFile:要上傳的文件路徑
* actionUrl:伺服器對應的程序路徑 */
// private String newName="345444.jpg";
private String uploadFile="/sdcard/345444.jpg";
private String actionUrl="http://*********/upload.PHP";
private TextView mText1;
private TextView mText2;
private Button mButton;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

mText1 = (TextView) findViewById(R.id.myText2);
mText1.setText("文件路徑:/n"+uploadFile);

mText2 = (TextView) findViewById(R.id.myText3);
mText2.setText("上傳網址:/n"+actionUrl);
/* 設定mButton的onClick事件處理 */
mButton = (Button) findViewById(R.id.myButton);
mButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
uploadFile();
}
});
}

/* 上傳文件吹Server的method */
private void uploadFile()
{
// String end = "/r/n";
// String twoHyphens = "--";
String boundary = "*****";
try
{
URL url =new URL(actionUrl);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
/* 允許Input、Output,不使用Cache */
// con.setReadTimeout(5 * 1000);
con.setDoInput(true);
con.setDoOutput(true);
con.setUseCaches(false);
/* 設定傳送的method=POST */
con.setRequestMethod("POST");
/* setRequestProperty */
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Charset", "UTF-8");
con.setRequestProperty("enctype",
"multipart/form-data;boundary="+boundary);
/* 設定DataOutputStream */
DataOutputStream ds =
new DataOutputStream(con.getOutputStream());
/*ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; " +
"name=/"file1/";filename=/"" +
newName +"/"" + end);
ds.writeBytes(end); */

/* 取得文件的FileInputStream */
FileInputStream fStream = new FileInputStream(uploadFile);
/* 設定每次寫入1024bytes */
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];

int length = -1;
/* 從文件讀取數據到緩沖區 */
while((length = fStream.read(buffer)) != -1)
{
/* 將數據寫入DataOutputStream中 */
ds.write(buffer, 0, length);
}
// ds.writeBytes(end);
// ds.writeBytes(twoHyphens + boundary + twoHyphens + end);

/* close streams */
fStream.close();
ds.flush();

/* 取得Response內容 */
InputStream is = con.getInputStream();
int ch;
StringBuffer b =new StringBuffer();
while( ( ch = is.read() ) != -1 )
{
b.append( (char)ch );
}
/* 將Response顯示於Dialog */
showDialog(b.toString().trim());
/* 關閉DataOutputStream */
ds.close();
}
catch(Exception e)
{
showDialog(""+e);
}
}

/* 顯示Dialog的method */
private void showDialog(String mess)
{
new AlertDialog.Builder(EX08_11.this).setTitle("Message")
.setMessage(mess)
.setNegativeButton("確定",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
}
})
.show();
}
}

PHP代碼

[php] view plain
$data = file_get_contents('php://input');
$handle = fopen($_SERVER['DOCUMENT_ROOT'].'/image/345444.jpg', 'w');
if ($handle)
{
fwrite($handle,$data);
fclose($handle);
echo "success";
}

2. 使用android上傳圖片到伺服器,並且把圖片保存到伺服器的某個文件夾里

有兩種方法,第一,把你的圖片轉成位元組流,然後用post方法把位元組流傳到服務端,然後服務端接收到位元組流之後,開啟一個線程把它重新壓縮成圖片,保存在某個文件夾下面。
第二,開啟一個線程,用socket直接把圖片放到stream中傳到服務端,服務端接收後保存到文件夾下。

3. Android圖片批量上傳的功能。(圖片比較大)

Android中上傳圖片或者下載圖片,使用最多的是xUtils和imageloader、glide,選用這兩種的哪一種框架都行,因為是批量和回圖片大容易造成界面卡以答及上傳速度慢,對圖片操作不當就容易造成OOM異常,一般對於批量上傳大圖片都需要對圖片也處理,然後在上傳第一步需要對圖片進行比例壓縮之後再進行質量壓縮,處理之後的圖片比之前的圖片會小很多,再加上框架的上傳處理,會有很好的效果,希望對你有所幫助

閱讀全文

與安卓開發圖片上傳代碼相關的資料

熱點內容
小米6檢測工具下載 瀏覽:540
桌面創建不了文件夾怎麼回事 瀏覽:894
qq古代頭像傷感圖片 瀏覽:797
wpspdf文檔如何在文件夾預覽 瀏覽:15
大數據與工業的結合包括哪些領域 瀏覽:993
蘋果7鬧鍾鈴聲設置歌曲 瀏覽:168
法庭文件有哪些 瀏覽:211
zip文件怎麼刪除嗎 瀏覽:247
win1016179安裝失敗 瀏覽:457
pp助手的文件系統 瀏覽:218
設計網站橫幅廣告要哪些因素 瀏覽:991
勞務報酬認定在多少號文件 瀏覽:209
本地數據傳輸業務有什麼用途 瀏覽:375
什麼情侶app好用 瀏覽:575
旅遊攻略適合下什麼app 瀏覽:403
nodejsbusboy下載 瀏覽:106
同事不會送文件找不到地方 瀏覽:473
銀聯萬事達applepay 瀏覽:834
試玩怎麼推廣app 瀏覽:588
課設報告中數據結構寫什麼東西 瀏覽:44

友情鏈接