A. android怎麼在伺服器和客戶端之間傳輸圖片
android客戶端和java服務端之間可以用socket來傳輸圖片。
伺服器端代碼:
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
public class Server02 {
public static void main(String[] args) {
try {
ServerSocket server = new ServerSocket(40000);
Socket socket = server.accept();
DataInputStream dos = new DataInputStream(socket.getInputStream());
int len = dos.available();
System.out.println("len = "+len);
byte[] data = new byte[len];
dos.read(data);
System.out.println("data = "+data);
dos.close();
socket.close();
server.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
客戶端代碼:
[java] view plain
imageView02 = (ImageView)findViewById(R.id.image02);
button02 = (Button)findViewById(R.id.Button02);
button02.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
Socket socket;
try {
socket = new Socket("192.168.1.203",40000);
DataOutputStream out = new DataOutputStream(socket.getOutputStream());
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.qt);
imageView02.setImageBitmap(bitmap);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
//讀取圖片到ByteArrayOutputStream
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] bytes = baos.toByteArray();
out.write(bytes);
System.out.println("bytes--->"+bytes);
out.close();
socket.close();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
B. 怎樣把安卓的照片上傳到php的伺服器
可以用php寫app介面給安卓軟體調用,比如寫一個圖片上傳介面,然後在圖片上傳的時候將信息傳入這介面就可以了。
C. 怎樣把安卓的照片上傳到php的伺服器
安裝一個上傳下載的軟體(如FTP),然後連接伺服器,之後吧圖片放到指定的文件夾即可。你可以去伺服器廠商(正睿)的網上找找相關的技術文檔參考一下,應該很快就清楚了!
D. 安卓如何在後台上傳圖片等資料
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";
}
E. 如何調用android的拍照或本地相冊選取,然後再實現相片上傳伺服器
首先是拍照:使用Intent即可,
[java] view plainprint?
01.final String start = Environment.getExternalStorageState();
02.private static final String PHOTOPATH = "/photo/";
03.
04.if(start.equals(Environment.MEDIA_MOUNTED)){
05.Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
06.File file = new File(Environment.getExternalStorageDirectory()+PHOTOPATH);
07.if(!file.exists()){
08.file.mkdirs();
09.}
10.tempphontname = System.currentTimeMillis()+".jpg";
11.buffer.append(Environment.getExternalStorageDirectory()+PHOTOPATH).append(tempphontname);
12.intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(buffer.toString())));
13.startActivityForResult(intent, 1);
14.}
final String start = Environment.getExternalStorageState();
private static final String PHOTOPATH = "/photo/";
if(start.equals(Environment.MEDIA_MOUNTED)){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory()+PHOTOPATH);
if(!file.exists()){
file.mkdirs();
}
tempphontname = System.currentTimeMillis()+".jpg";
buffer.append(Environment.getExternalStorageDirectory()+PHOTOPATH).append(tempphontname);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(buffer.toString())));
startActivityForResult(intent, 1);
}
其次是從本地相冊選:依舊是Intent.
如下代碼:
[java] view plainprint?
01.if(start.equals(Environment.MEDIA_MOUNTED)){
02. Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
03. getImage.addCategory(Intent.CATEGORY_OPENABLE);
04. getImage.setType("image/jpeg");
05. startActivityForResult(getImage, 0);
06.}
if(start.equals(Environment.MEDIA_MOUNTED)){
Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
getImage.addCategory(Intent.CATEGORY_OPENABLE);
getImage.setType("image/jpeg");
startActivityForResult(getImage, 0);
}
接下來是主要的:因為調用完系統的方法後,回返回到回調方法onActivityResult(int, int, Intent)中,
在裡面進行主要的照片上傳伺服器的操作,
見代碼:
[java] view plainprint?
01.@Override
02. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
03. ContentResolver resolver = getContentResolver();
04. if(requestCode==1)//
05. {
06. if(resultCode==Activity.RESULT_OK)
07. {
08. if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
09. {
10.ew Thread(new Runnable()
11. {
12. @Override
13. public void run() {
14.//進行上傳操作
15.}
16.}.start();
轉載
F. android中如何上傳圖片到FTP伺服器
android客戶端實現FTP文件需要用到 commons-net-3.0.1.jar
先將jar包復制到android libs目錄下
復制以下實現代碼
以下為實現代碼:
/**
* 通過ftp上傳文件
* @param url ftp伺服器地址 如:
* @param port 埠如 :
* @param username 登錄名
* @param password 密碼
* @param remotePath 上到ftp伺服器的磁碟路徑
* @param fileNamePath 要上傳的文件路徑
* @param fileName 要上傳的文件名
* @return
*/
public String ftpUpload(String url, String port, String username,String password, String remotePath, String fileNamePath,String fileName) {
FTPClient ftpClient = new FTPClient();
FileInputStream fis = null;
String returnMessage = "0";
try {
ftpClient.connect(url, Integer.parseInt(port));
boolean loginResult = ftpClient.login(username, password);
int returnCode = ftpClient.getReplyCode();
if (loginResult && FTPReply.isPositiveCompletion(returnCode)) {// 如果登錄成功
ftpClient.makeDirectory(remotePath);
// 設置上傳目錄
ftpClient.changeWorkingDirectory(remotePath);
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("UTF-8");
ftpClient.enterLocalPassiveMode();
fis = new FileInputStream(fileNamePath + fileName);
ftpClient.storeFile(fileName, fis);
returnMessage = "1"; //上傳成功
} else {// 如果登錄失敗
returnMessage = "0";
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("FTP客戶端出錯!", e);
} finally {
//IOUtils.closeQuietly(fis);
try {
ftpClient.disconnect();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("關閉FTP連接發生異常!", e);
}
}
return returnMessage;
}
G. 怎樣把安卓的照片上傳到php的伺服器
只需要一些上傳工具,如(FTP等),傳到指定的文件裡面,然後調用即可。
H. Android 上傳圖片到伺服器
http://192.168.1.212:8011/pd/upload/fileUpload.do;
這個是伺服器地址,你圖片要上傳的地方。。
理論上是需要一個伺服器接收你上傳的圖片的!
他這個demo中的url是本地的,目測是寫demo的人自己寫的用來測試的地址
I. android手機裡面的照片能批量上傳到伺服器嗎
這個圖片存放的位置是根據你的圖片來源而定的。一般是放在sdcard下的某個目錄下的,
。我來給你說下思路:服務端(android手機)這邊需要寫個工具類,來遍歷SD卡下的文件,只顯示jpg和png的圖片。主類中有個按鈕來添加圖片,還有一個按鈕是用來上傳圖片,然後寫個監聽,用來接收服務端發回的消息。...服務端這邊寫個監聽來接收客戶端發來的消息,保存發過來的數據流。至於手機上能顯示這張圖片,只要在寫個imageview,把圖片資源載入上就ok啦,你可以去網上搜索一下「sd上的文件上傳」,希望可以幫到你哦哦