導航:首頁 > 文字圖片 > android上傳圖片文字

android上傳圖片文字

發布時間:2022-08-04 20:15:30

① android 上傳圖片或文件都是怎麼弄的

一:可以使用httppost上傳文件
二:或者socket寫入文件
上面兩種都可以獲得二進制流,然後把文件寫入流,這一類網路操作最好使用非同步任務模型

② android的自帶的httpClient 怎麼上傳文件

Android上傳文件到服務端可以使用HttpConnection 上傳文件,也可以使用Android封裝好的HttpClient類。當僅僅上傳文件可以直接使用httpconnection 上傳比較方便快捷。

1、使用HttpConection上傳文件。將文件轉換成表單數據流。主要的思路就自己構造個http協議內容,服務端解析報文獲得表單數據。代碼片段:

[java] view plain
HttpURLConnection con;
try {
con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(C_TimeOut);
/* 允許Input、Output,不使用Cache */
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("Content-Type","multipart/form-data;boundary=" + boundary);

/* 設置DataOutputStream */
DataOutputStream ds = new DataOutputStream(con.getOutputStream());
FileInputStream fStream = new FileInputStream(file);

/* 設置每次寫入1024bytes */
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];

int length = -1;
/* 從文件讀取數據至緩沖區 */
while((length = fStream.read(buffer)) != -1)
{
/* 將資料寫入DataOutputStream中 */
ds.write(buffer, 0, length);
}
fStream.close();
ds.flush();
ds.close();

可以參考

①《在 Android 上通過模擬 HTTP multipart/form-data 請求協議信息實現圖片上傳》 (http://bertlee.iteye.com/blog/1134576)。

②《關於android Http訪問,上傳,用了三個方法 》

2、使用Android HttpClient類上傳參數。下面我在網上搜到得代碼,忘記出處了

[java] view plain
private static boolean sendPOSTRequestHttpClient(String path,
Map<String, String> params) throws Exception {
// 封裝請求參數
List<NameValuePair> pair = new ArrayList<NameValuePair>();
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
pair.add(new BasicNameValuePair(entry.getKey(), entry
.getValue()));
}
}
// 把請求參數變成請求體部分
UrlEncodedFormEntity uee = new UrlEncodedFormEntity(pair, "utf-8");
// 使用HttpPost對象設置發送的URL路徑
HttpPost post = new HttpPost(path);
// 發送請求體
post.setEntity(uee);
// 創建一個瀏覽器對象,以把POST對象向伺服器發送,並返回響應消息
DefaultHttpClient dhc = new DefaultHttpClient();
HttpResponse response = dhc.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {
Log.i("http", "httpclient");
return true;
}
return false;
}

3、使用httpClient上傳文字信息和文件信息。使用httpClient上傳文件非常的方便。不過需要導入apache-mime4j-0.6.jar 和httpmime-4.0.jar兩個.jar包。

[java] view plain
// 封裝請求參數
MultipartEntity mpEntity = new MultipartEntity();
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {

StringBody par = new StringBody(entry.getValue());
mpEntity.addPart(entry.getKey(), par);
}
}
// 圖片
if (!imagepath.equals("")) {
FileBody file = new FileBody(new File(imagepath));
mpEntity.addPart("photo", file);
}
// 使用HttpPost對象設置發送的URL路徑
HttpPost post = new HttpPost(path);
// 發送請求體
post.setEntity(mpEntity);
// 創建一個瀏覽器對象,以把POST對象向伺服器發送,並返回響應消息
DefaultHttpClient dhc = new DefaultHttpClient();
HttpResponse response = dhc.execute(post);

FileBody類可以把文件封裝到表單中,實現附件的上傳。

關於httpClient上傳文件可以參考鏈接: http://www.eoeandroid.com/forum.php?mod=viewthread&tid=76721&page=1

需要用的的ja下載地址r:http://download.csdn.net/detail/china1988s/3791514

參考:

①《在 Android 上通過模擬 HTTP multipart/form-data 請求協議信息實現圖片上傳》 (http://bertlee.iteye.com/blog/1134576)。

②《關於android Http訪問,上傳,用了三個方法 》

③ Android聊天界面 傳文字,傳圖片,傳語音

語音和圖片其實都是傳文件。
發送方將圖片和語音發送至伺服器,獲取到一個url,然後通過消息把url發送過去。
接收方根據得到的url顯示圖片或者點擊時播放語音(語音可以考慮先下載至本地)

④ Android如何在Html中插入圖片和文字

android在html插入圖片和文字的方式如下:

  1. 叫前端工程師,設計好html界面,裡面已經插入了圖片和文字。

  2. 將所有的html文件和css文件放到android工程的asserts文件夾下,在代碼中就可以載入文件夾下的所有html文件。

⑤ android怎樣實現在圖片上任意位置添加文字 要代碼 急求。。

android在圖片上任意位置添加文字,可以使用canvas類進行操作,實例如下:

@Override
protectedvoidonDraw(Canvascanvas)
{
super.onDraw(canvas);
canvas.drawBitmap(m_LogInBmp,null,m_LogIndst,m_txBackPaint);

floattleft=m_LogIndst.left+(m_LogIndst.right-m_LogIndst.left-FontWidth*m_TextLogIn.length())/2;
floatttop=m_LogIndst.top+(m_LogIndst.bottom-m_LogIndst.top-FontHeight)/2+FontHeight;
canvas.drawText(m_TextLogIn,tleft,ttop,mLogInPaint);
}

⑥ 怎樣在android系統中上傳圖片,信息。以及修改信息,這些代碼

我一般都是通過遍歷集合的方式來上傳圖片。而且一般都不會去管這個上傳的順序,只需要服務端按你需要返回數據就可以了

⑦ android 客戶端開發 如何同時上傳多張照片

1、在微博頁面點擊左上角發布按鈕後,點擊「照相機」標識或「圖片」標識;
2、選擇圖片進行上傳,選定後點擊右下角的綠色「確認」按鈕
3、多圖上傳最多支持9張圖片,如果還需添加可點擊「十」字繼續選擇上傳,如果添加完畢可點擊右上角的藍色「發布」即可。

⑧ android 中寫入幾個圖片 點擊圖片後有相應的文字介紹該圖片 該怎麼寫急急。。。。。。。

你想要什麼效果呢。。。如果是隨便,就弄個textview組件setText就行了。
如果想要彈出效果就用builder。具體代碼如下:
Builder builder=new AlertDialog.Builder(this);
builder.setMessage("這個圖片OOXXXXX");
builder.setPositiveButton("確定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
}).create().show();

把這段代碼放進放圖片的view的點擊事件內就ok了。
點擊事件不用我說了吧。。。。on什麼什麼的。。。自己看相應的需求選擇監聽事件吧。

⑨ android 實時添加圖片和文字到listview,如何實現

在activity裡面調用addOne就可以了。
圖片和文字的顯示需要自己寫layiout文件R.layout.music_playlist_item

public class MusicPlayListAdapter extends BaseAdapter {

List<? extends PlayListInfo> data;

public MusicPlayListAdapter(List<? extends PlayListInfo> data) {
this.data = data;
}

public void addOne(PlayListInfo music) {
data.add(music);
notifyDataSetChanged();
}

@Override
public int getCount() {
return data.size();
}

@Override
public Object getItem(int position) {
return data.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.music_playlist_item, null);
}

PlayListInfo music = data.get(position);
........

return convertView;
}

}

⑩ android如何實現圖片批量上傳

首先,以下架構下的批量文件上傳可能會失敗或者不會成功:
1.android客戶端+springMVC服務端:服務端採用org.springframework.web.multipart.MultipartHttpServletRequest作為批量上傳接收類,這種搭配下的批量文件上傳會失敗,最終服務端只會接受到一個文件,即只會接受到第一個文件。可能因為MultipartHttpServletRequest對servlet原本的HttpServletRequest類進行封裝,導致批量上傳有問題。
2.android客戶端+strutsMVC服務端:
上傳成功的方案:
採用android客戶端+Servlet(HttpServletRequest)進行文件上傳。
Servlet端代碼如下:

[java] view plainprint?
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
try
{
List items = upload.parseRequest(request);
Iterator itr = items.iterator();
while (itr.hasNext())
{
FileItem item = (FileItem) itr.next();
if (item.isFormField())
{
System.out.println("表單參數名:" + item.getFieldName() + ",表單參數值:" + item.getString("UTF-8"));
}
else
{
if (item.getName() != null && !item.getName().equals(""))
{
System.out.println("上傳文件的大小:" + item.getSize());
System.out.println("上傳文件的類型:" + item.getContentType());
// item.getName()返回上傳文件在客戶端的完整路徑名稱
System.out.println("上傳文件的名稱:" + item.getName());

File tempFile = new File(item.getName());
// 上傳文件的保存路徑
File file = new File(sc.getRealPath("/") + savePath, tempFile.getName());
item.write(file);
request.setAttribute("upload.message", "上傳文件成功!");
} else
{
request.setAttribute("upload.message", "沒有選擇上傳文件!");
}
}
}
}
catch (FileUploadException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
request.setAttribute("upload.message", "上傳文件失敗!");
}
request.getRequestDispatcher("/uploadResult.jsp").forward(request, response);

android端代碼如下:

[java] view plainprint?
public class SocketHttpRequester {
/**
*多文件上傳
* 直接通過HTTP協議提交數據到伺服器,實現如下面表單提交功能:
* <FORM METHOD=POST ACTION="http://192.168.1.101:8083/upload/servlet/UploadServlet" enctype="multipart/form-data">
<INPUT TYPE="text" NAME="name">
<INPUT TYPE="text" NAME="id">
<input type="file" name="imagefile"/>
<input type="file" name="zip"/>
</FORM>
* @param path 上傳路徑(註:避免使用localhost或127.0.0.1這樣的路徑測試,因為它會指向手機模擬器,你可以使用http://www.iteye.cn或http://192.168.1.101:8083這樣的路徑測試)
* @param params 請求參數 key為參數名,value為參數值
* @param file 上傳文件
*/
public static boolean post(String path, Map<String, String> params, FormFile[] files) throws Exception{
final String BOUNDARY = "---------------------------7da2137580612"; //數據分隔線
final String endline = "--" + BOUNDARY + "--\r\n";//數據結束標志

int fileDataLength = 0;
for(FormFile uploadFile : files){//得到文件類型數據的總長度
StringBuilder fileExplain = new StringBuilder();
fileExplain.append("--");
fileExplain.append(BOUNDARY);
fileExplain.append("\r\n");
fileExplain.append("Content-Disposition: form-data;name=\""+ uploadFile.getParameterName()+"\";filename=\""+ uploadFile.getFilname() + "\"\r\n");
fileExplain.append("Content-Type: "+ uploadFile.getContentType()+"\r\n\r\n");
fileExplain.append("\r\n");
fileDataLength += fileExplain.length();
if(uploadFile.getInStream()!=null){
fileDataLength += uploadFile.getFile().length();
}else{
fileDataLength += uploadFile.getData().length;
}
}
StringBuilder textEntity = new StringBuilder();
for (Map.Entry<String, String> entry : params.entrySet()) {//構造文本類型參數的實體數據
textEntity.append("--");
textEntity.append(BOUNDARY);
textEntity.append("\r\n");
textEntity.append("Content-Disposition: form-data; name=\""+ entry.getKey() + "\"\r\n\r\n");
textEntity.append(entry.getValue());
textEntity.append("\r\n");
}
//計算傳輸給伺服器的實體數據總長度
int dataLength = textEntity.toString().getBytes().length + fileDataLength + endline.getBytes().length;

URL url = new URL(path);
int port = url.getPort()==-1 ? 80 : url.getPort();
Socket socket = new Socket(InetAddress.getByName(url.getHost()), port);
OutputStream outStream = socket.getOutputStream();
//下面完成HTTP請求頭的發送
String requestmethod = "POST "+ url.getPath()+" HTTP/1.1\r\n";
outStream.write(requestmethod.getBytes());
String accept = "Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*\r\n";
outStream.write(accept.getBytes());
String language = "Accept-Language: zh-CN\r\n";
outStream.write(language.getBytes());
String contenttype = "Content-Type: multipart/form-data; boundary="+ BOUNDARY+ "\r\n";
outStream.write(contenttype.getBytes());
String contentlength = "Content-Length: "+ dataLength + "\r\n";
outStream.write(contentlength.getBytes());
String alive = "Connection: Keep-Alive\r\n";
outStream.write(alive.getBytes());
String host = "Host: "+ url.getHost() +":"+ port +"\r\n";
outStream.write(host.getBytes());
//寫完HTTP請求頭後根據HTTP協議再寫一個回車換行
outStream.write("\r\n".getBytes());
//把所有文本類型的實體數據發送出來
outStream.write(textEntity.toString().getBytes());
//把所有文件類型的實體數據發送出來
for(FormFile uploadFile : files){
StringBuilder fileEntity = new StringBuilder();
fileEntity.append("--");
fileEntity.append(BOUNDARY);
fileEntity.append("\r\n");
fileEntity.append("Content-Disposition: form-data;name=\""+ uploadFile.getParameterName()+"\";filename=\""+ uploadFile.getFilname() + "\"\r\n");
fileEntity.append("Content-Type: "+ uploadFile.getContentType()+"\r\n\r\n");
outStream.write(fileEntity.toString().getBytes());
if(uploadFile.getInStream()!=null){
byte[] buffer = new byte[1024];
int len = 0;
while((len = uploadFile.getInStream().read(buffer, 0, 1024))!=-1){
outStream.write(buffer, 0, len);
}
uploadFile.getInStream().close();
}else{
outStream.write(uploadFile.getData(), 0, uploadFile.getData().length);
}
outStream.write("\r\n".getBytes());
}
//下面發送數據結束標志,表示數據已經結束
outStream.write(endline.getBytes());

BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
if(reader.readLine().indexOf("200")==-1){//讀取web伺服器返回的數據,判斷請求碼是否為200,如果不是200,代表請求失敗
return false;
}
outStream.flush();
outStream.close();
reader.close();
socket.close();
return true;
}

/**
*單文件上傳
* 提交數據到伺服器
* @param path 上傳路徑(註:避免使用localhost或127.0.0.1這樣的路徑測試,因為它會指向手機模擬器,你可以使用http://www.itcast.cn或http://192.168.1.10:8080這樣的路徑測試)
* @param params 請求參數 key為參數名,value為參數值
* @param file 上傳文件
*/
public static boolean post(String path, Map<String, String> params, FormFile file) throws Exception{
return post(path, params, new FormFile[]{file});
}
}

閱讀全文

與android上傳圖片文字相關的資料

熱點內容
傷感男動漫高清圖片 瀏覽:417
淘寶圖片檢測文字 瀏覽:740
圖片如何與文檔模板融合 瀏覽:142
簡單可愛圖片動漫女生 瀏覽:363
燙發短發中卷發型圖片女生 瀏覽:831
晚安健身圖片高清 瀏覽:714
吶喊可愛圖片女生 瀏覽:121
word文檔版本不同顯示不一樣圖片 瀏覽:398
miumiu手包價格和圖片 瀏覽:669
lv新款女包圖片2015及價格查詢 瀏覽:374
貓的動漫萌圖高清圖片 瀏覽:787
關於簡單健康的手抄報圖片 瀏覽:88
天太熱了動漫圖片 瀏覽:635
男生傷心的哭圖片 瀏覽:537
word圖片設置格式嵌入型 瀏覽:433
文字圖片排版高級感 瀏覽:133
簡單水彩寫生圖片 瀏覽:983
美女背後表白圖片 瀏覽:119
男發後面發型圖片 瀏覽:376
可愛的蝸牛圖片簡筆畫 瀏覽:832