gpt4 book ai didi

android - 如何使用volley从android上传用户指定数量的图像或文档?

转载 作者:行者123 更新时间:2023-11-29 19:03:29 25 4
gpt4 key购买 nike

我的应用程序获取图像(强制)和一些文本,并将图像上传到服务器,并将图像位置和文本保存在 mysql 数据库中。我希望用户可以根据需要上传多个图像/文档(pdf、doc)要求诸如添加按钮之类的东西来增加上传字段。如何在数据库中为每个上传的文件存储多条记录(我需要为每次上传创建多个字段)?如何使用添加按钮为新文档创建上传字段并为每个上传请求实现java(用户可以不上传文件)?以及如何在 php 中处理此请求?

我上传单张图片的工作

public class NewsUpload extends Activity implements View.OnClickListener{

Button chooseImage;
Button uploadImage;
EditText titleView;
EditText paragraphView;
ImageView imageView;
Bitmap bitmap;

public static final String UPLOAD_URL = "myurl.com/upload.php";
public static final String KEY_IMAGE ="image";
public static final String KEY_TITLE ="title";
public static final String KEY_PARAGRAPH ="paragraph";
private int PICK_IMAGE_REQUEST = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.upload_news);
Intent intent = getIntent();
String name = intent.getStringExtra("username");
Toast.makeText(this,name,Toast.LENGTH_SHORT).show();
chooseImage = (Button)findViewById(R.id.buttonChoose);
uploadImage = (Button) findViewById(R.id.buttonUpload);
chooseImage.setOnClickListener(this);
uploadImage.setOnClickListener(this);
titleView = (EditText) findViewById(R.id.titleUpload);
paragraphView = (EditText) findViewById(R.id.paragraphUpload);
imageView = (ImageView)findViewById(R.id.imageUploadView);
}

public String getStringImage(Bitmap bmp){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}

private void uploadImage(){
final ProgressDialog loading = ProgressDialog.show(this,"Uploading...","Please wait...",false,false);
StringRequest stringRequest = new StringRequest(Request.Method.POST, UPLOAD_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String s) {
loading.dismiss();
Toast.makeText(NewsUpload.this, s, Toast.LENGTH_LONG).show();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError){
loading.dismiss();
Toast.makeText(NewsUpload.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
String image = getStringImage(bitmap);
String title = titleView.getText().toString().trim();
String paragraph = paragraphView.getText().toString().trim();
Map<String,String> params = new Hashtable<String, String>();
params.put(KEY_IMAGE, image);
params.put(KEY_TITLE, title);
params.put(KEY_PARAGRAPH,paragraph);
return params;
}
};
RequestQueue requestQueue = MySingleton.getInstance(this).getRequestQueue(); // Mysingleton => Instance of Volley Class implemented seperately.
stringRequest.setRetryPolicy(new DefaultRetryPolicy(20*1000,0,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(stringRequest);
}

private void showFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri filePath = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onClick(View v) {
if(v.getId()==chooseImage.getId()){
showFileChooser();
}

if(v.getId() == uploadImage.getId()){
uploadImage();
}
}

}

最佳答案

我已将图像从手机上传到 NodeJS 服务器实际上我没有粘贴所有代码,因为最重要的是传输位图到字节

**

img_perfil.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
logout.setText("Guardar Imagen");
intent.setAction(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");

startActivityForResult(intent, RQS_OPEN_IMAGE);
}
});



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {


if (requestCode == RQS_OPEN_IMAGE ||
requestCode == RQS_GET_IMAGE ||
requestCode == RQS_PICK_IMAGE) {

img_perfil.setImageBitmap(null);


Uri mediaUri = data.getData();
// textInfo1.setText(mediaUri.toString());
String mediaPath = mediaUri.getPath();
//textInfo2.setText(mediaPath);

//display the image
try {
inputStream = getBaseContext().getContentResolver().openInputStream(mediaUri);
bm = BitmapFactory.decodeStream(inputStream);
img_perfil.setImageBitmap(bm);
img_perfil.setScaleType(ImageView.ScaleType.FIT_START);
img_perfil.setAdjustViewBounds(true);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
}

ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100,bos);
byte[] b= bos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.NO_WRAP | Base64.URL_SAFE | Base64.NO_PADDING);
new CargarDatos().execute("CHAT_SERVER_URL"+encodedImage,METHOD_POST);

关于android - 如何使用volley从android上传用户指定数量的图像或文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43688817/

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