gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:10:37 25 4
gpt4 key购买 nike

我想将图像和文本上传到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中存储路径,但无法存储文本。请帮助我。

最佳答案

public int uploadFile(String sourceFileUri) {使用

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

相反

String des=tv.toString();

关于android - 将图像和文本上传到 MySQL (android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24104222/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com