开发者

How can i implement the possibility of select an image on my app and send it to a remote database (JSON+PHP)?

i am creating an app that is like a mini social network app, with users, that can be friends of other users. Each user haves email, fullName, etc... but now i want to implement and give to the user the possibility to select a image from the images directory of the phone and make it his profile image.

That image have to be sent to the remote server and all the other users will see that image when open the description of that friend.

On my app i am using JSON and PHP to connect from my app to the remote database, then i supose that i have to transform the image into a String and add a row on the remote database representing the String of the image.

I have no idea of how to do this, and i can't find any easy help on google or here, all i am finding is too hard for me or is not what i am searching for.

Code examples are wel开发者_开发问答come


Please do not ever store a file directly inside a database field. The file belongs on disk in a directory on the server. A simple how to. All that is stored in the database is the file location (a string) - this keeps your tables small and helps increase query read times.

To upload from Android you need to send it as a byte stream.

Some sample code

HttpURLConnection conn = null; DataOutputStream dos = null; DataInputStream inStream = null;
String exsistingFileName = "path_to_file";

                 String lineEnd = "\r\n";
                 String twoHyphens = "--";
                 String boundary =  "*****";

                 int bytesRead, bytesAvailable, bufferSize;
                 byte[] buffer;
                 int maxBufferSize = 1*1024*1024;
                 String urlString = urls[i];


                 //get the file size here
                 File f = new File(exsistingFileName);
                 long fileSize = f.length();
                 FileInputStream fileInputStream = new FileInputStream(f);

                  // open a URL connection to the Servlet
                  URL url = new URL(urlString);

                  // Open a HTTP connection to the URL
                  conn = (HttpURLConnection) url.openConnection();
                  //add the cookie

                  // Allow Inputs
                  conn.setDoInput(true);

                  // Allow Outputs
                  conn.setDoOutput(true);

                  // Don't use a cached copy.
                  conn.setUseCaches(false);

                  // Use a post method.
                  conn.setRequestMethod("POST");
                  conn.setRequestProperty("Connection", "Keep-Alive");
                  conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

                  dos = new DataOutputStream( conn.getOutputStream() );
                  dos.writeBytes(twoHyphens + boundary + lineEnd);
                  dos.writeBytes("Content-Disposition: form-data; name=\"file\";filename=\"" + exsistingFileName +"\"" + lineEnd);
                  dos.writeBytes("Content-Type: image/jpeg"+ lineEnd); 
                  dos.writeBytes(lineEnd);

                  // create a buffer of maximum size
                  bytesAvailable = fileInputStream.available();
                  bufferSize = Math.min(bytesAvailable, maxBufferSize);
                  buffer = new byte[bufferSize];

                  // read file and write it into form...
                  bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                  while (bytesRead > 0)
                  {
                      dos.write(buffer, 0, bufferSize);
                      bytesAvailable = fileInputStream.available();
                      bufferSize = Math.min(bytesAvailable, maxBufferSize);
                      bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                  }
                  // send multipart form data necesssary after file data...
                  dos.writeBytes(lineEnd);
                  dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                  // close streams
                  fileInputStream.close();
                  dos.flush();
                  dos.close();

                 //------------------ read the SERVER RESPONSE

                   inStream = new DataInputStream ( conn.getInputStream() );
                   String str;
                   while (( str = inStream.readLine()) != null)
                   {
                        Log.d(TAG,"Server Response"+str);
                   }
                   inStream.close();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜