将图像和文本上传到MySQL(android)

时间:2014-06-08 08:12:58

标签: android mysql

我想将图片和文字上传到MySQL,这是我的代码:

 public class UploadActivity extends Activity {


private static int RESULT_LOAD_IMAGE = 1;
String picturePath;
private EditText tv;
ProgressDialog dialog = null;
int serverResponseCode = 0;
String serverResponseMessage;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    EditText des=(EditText)findViewById(R.id.tv);
    Button b = (Button)findViewById(R.id.upload);   
    Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);

    buttonLoadImage.setOnClickListener(new View.OnClickListener() {


        public void onClick(View arg0) {

            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });


b.setOnClickListener(new OnClickListener() {           

    public void onClick(View v) {
        dialog = ProgressDialog.show(ImageGalleryDemoActivity.this, "", "Uploading file...", true);
         new Thread(new Runnable() {
                public void run() {
                     runOnUiThread(new Runnable() {
                            public void run() {
                               // tv.setText("uploading started.....");
                            }
                        });                     
                 int response= uploadFile(picturePath);
                 System.out.println("RES : " + response);                        
                }




                public int uploadFile(String sourceFileUri) {


                    String upLoadServerUri = "http://10.0.2.2/rest/upload.php";
                    String fileName = sourceFileUri;
                    HttpURLConnection conn = null;
                    DataOutputStream dos = null; 
                    String lineEnd = "\r\n";
                    String twoHyphens = "--";
                    String boundary = "*****";
                    String des=tv.toString();

                    int bytesRead, bytesAvailable, bufferSize;
                    byte[] buffer;
                    int maxBufferSize = 1 * 1024 * 1024;
                    File sourceFile = new File(sourceFileUri);

                    if (!sourceFile.isFile()) {
                     Log.e("uploadFile", "Source File Does not an existan");
                     return 0;
                    }
                        try { 
                         FileInputStream fileInputStream = new FileInputStream(sourceFile);
                         URL url = new URL(upLoadServerUri);
                         conn = (HttpURLConnection) url.openConnection(); 
                         conn.setDoInput(true); 
                         conn.setDoOutput(true);
                         conn.setUseCaches(false);
                         conn.setRequestMethod("POST");
                         conn.setRequestProperty("Connection", "Keep-Alive");
                         conn.setRequestProperty("ENCTYPE", "multipart/form-data");
                         conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);
                         conn.setRequestProperty("uploaded_file", fileName);
                         conn.setRequestProperty("des", des);

                         dos = new DataOutputStream(conn.getOutputStream());

                         dos.writeBytes(twoHyphens + boundary + lineEnd);
                         dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\""+ fileName+"\"" + lineEnd);
                         dos.writeBytes("Content-Disposition: form-data; name=\"des\";filename=\""+ des+"\"" + lineEnd);
                         dos.writeBytes(lineEnd);

                         bytesAvailable = fileInputStream.available(); buffer of  maximum size

                         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);              
                          }


                         dos.writeBytes(lineEnd);
                         dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

                         serverResponseCode = conn.getResponseCode();
                         serverResponseMessage = conn.getResponseMessage();
                         Map<String, List<String>> temp = conn.getHeaderFields();

                         System.out.println("anand===>"+temp.toString());
                         Log.i("uploadFile", "an class=" + serverResponseMessage + ": " + serverResponseCode);
                         if(serverResponseCode == 200){
                             runOnUiThread(new Runnable() {
                                  public void run() {

                                      Toast.makeText(ImageGalleryDemoActivity.this, "File Upload Complete."+serverResponseMessage, Toast.LENGTH_SHORT).show();
                                  }
                              });               
                         }   

                        **strong text**
                         fileInputStream.close();
                         dos.flush();
                         dos.close();

                    } catch (MalformedURLException ex) { 
                        dialog.dismiss(); 
                        ex.printStackTrace();
                        Toast.makeText(ImageGalleryDemoActivity.this, "MalformedURLException", Toast.LENGTH_SHORT).show();
                        Log.e("Upload file to server", "error: " + ex.getMessage(), ex); 
                    } catch (Exception e) {
                        dialog.dismiss(); 
                        e.printStackTrace();
                        Toast.makeText(ImageGalleryDemoActivity.this, "Exception : " + e.getMessage(), Toast.LENGTH_SHORT).show();
                        Log.e("Upload file to server Exception", "Exception : " + e.getMessage(), e); 
                    }
                    dialog.dismiss();      
                    return serverResponseCode; 
                   }




              }).start();       
        }
});
  }

它只能将图像存储在服务器中并将路径存储在MySQL中,但它无法存储文本。请帮帮我。

1 个答案:

答案 0 :(得分:0)

public int uploadFile(String sourceFileUri) { 使用

String des=tv.getText().toString();

代替

String des=tv.toString();