【问题标题】:take picture from intent android and send to server java从intent android拍照并发送到服务器java
【发布时间】: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


【解决方案1】:

不要对文件输出流中的文件名进行硬编码。很可能是不正确的。

使用

Uri uri = data.getData();

获取所拍图片的uri。 “data”是onActivityResult方法的Intent参数。

或者使用:

String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null);
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();

//THIS IS WHAT YOU WANT!
String capturedImageFilePath = cursor.getString(column_index_data);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多