【发布时间】:2012-10-20 12:12:57
【问题描述】:
我最近开始学习 Android,我想制作一个应用程序,从相机意图中拍摄照片并将其发送到服务器,但我只能拍照。有人可以帮助我吗? 现在放置代码。 客户...
public class SendPhoto extends Activity {
private int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
int i;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(), "temp/test.jpg");
Uri outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
int i;
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
try{
FileInputStream fis = new FileInputStream("sd/temp/test.jpg");
Socket sock = new Socket ("hostname",3333);
DataOutputStream os = new DataOutputStream(sock.getOutputStream());
while ((i = fis.read()) > -1)
os.write(i);
fis.close();
os.close();
sock.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
服务器...
public class Server {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
ServerSocket socket = new ServerSocket(3333);
System.out.println("Server started. Listening to the port 3333");
while (true) {
Socket clientSocket = socket.accept();
DataInputStream dis = new DataInputStream(clientSocket.getInputStream());
FileOutputStream fout = new FileOutputStream("output.jpg");
int i;
while ( (i = dis.read()) > -1) {
fout.write(i);
}
DataOutputStream outToClient= new DataOutputStream(clientSocket.getOutputStream());
outToClient.writeBytes("Hello");
fout.flush();
fout.close();
dis.close();
clientSocket.close();
}
}
}
谢谢。
【问题讨论】:
-
你能更准确地说出你想要什么
-
我无法将照片发送到服务器
-
从尝试“localhost”而不是“hostname”开始
-
我更改了名称以使其清楚...在我的应用程序中,我输入了运行服务器程序的计算机的名称。这不是问题。
-
对不起请不要提交我的编辑..我犯了一些错误
标签: android android-intent image-capture