我完成了一次可以一次将一个图像上传到PHP的代码,但是我不知道如何一次一次上传多个图像。我知道我必须设置更多的imageView和按钮进行选择,但是我无法理解其中的一些代码。例如,如何为更多图像设置位图,光标,filePath或路径?请帮助我。
package com.example.u9526.newdevil;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import net.gotev.uploadservice.MultipartUploadRequest;
import net.gotev.uploadservice.UploadNotificationConfig;
import java.io.IOException;
import java.util.UUID;
public class test extends AppCompatActivity implements View.OnClickListener{
private ImageView image1;
private Button choose1, upload;
private ProgressDialog progressDialog;
private static final int STORAGE_PERMISSION_CODE = 2342;
private static final int PICK_IMAGE_REQUEST = 22;
private Uri filePath1;
private Bitmap bitmap1;
private static final String URL_UPLOAD = "http://10.0.2.2/UploadExample/upload2.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
requestStoragePermission();
choose1 = (Button) findViewById(R.id.choose1);
upload = (Button) findViewById(R.id.upload);
image1 = (ImageView) findViewById(R.id.image1);
choose1.setOnClickListener(this);
upload.setOnClickListener(this);
}
private void requestStoragePermission() {
if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
return;
ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_CODE);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == STORAGE_PERMISSION_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "Permission granted", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(this, "Permission not granted", Toast.LENGTH_LONG).show();
}
}
}
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, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath1 = data.getData();
try {
bitmap1 = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath1);
image1.setImageBitmap(bitmap1);
} catch (IOException e) {
}
}
}
private String getPath(Uri uri){
Cursor cursor = getContentResolver().query(uri, null , null, null, null);
cursor.moveToFirst();
String document_id = cursor.getString(0);
document_id = document_id.substring(document_id.lastIndexOf(":")+1);
cursor.close();
cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, MediaStore.Images.Media._ID + " = ? ",
new String[] {document_id},null
);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
return path;
}
private void UploadImage() {
String path = getPath(filePath1);
try {
String uploadid = UUID.randomUUID().toString();
new MultipartUploadRequest(this, uploadid, URL_UPLOAD)
.addFileToUpload(path, "image")
.setNotificationConfig(new UploadNotificationConfig())
.setMaxRetries(2)
.startUpload();
} catch (Exception e) {
}
}
@Override
public void onClick(View v) {
if(v == upload){
UploadImage();
}
if(v == choose1 ){
showFileChooser();
}
}
}
这是我用于上传图片的PHP文件。
<?php
header("Content-Type:text/html; charset=utf-8");
require_once 'dbDetails.php';
$upload_path = 'uploads/';
$server_ip = gethostbyname(gethostname());
$upload_url = 'http://'.$server_ip.'/UploadExample/'.$upload_path;
$response = array();
if($_SERVER['REQUEST_METHOD'] == 'POST'){
if(isset($_POST['name']) and isset($_FILES['image']['name'])){
$con = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME) or die('unable to connect to database ');
$name = $_POST['name'];
$fileinfo = pathinfo($_FILES['image']['name']);
$extension = $fileinfo['extension'];
$file_url = $upload_url . getFileName() . '.'.$extension;
$file_path = $upload_path. getFileName(). '.'.$extension;
try{
move_uploaded_file($_FILES['image']['tmp_name'], $file_path);
$sql = "INSERT INTO images (url, name) VALUES ('$file_url','$name')";
if(mysqli_query($con,$sql)){
$response['error'] = false;
$response['url'] = $file_url;
$response['name'] = $name;
}
}catch(Exception $e){
$response['error'] = false;
$response['message'] = $e->getMessage();
}
mysqli_close($con);
}else{
$response['error'] = true;
$response['message'] = 'please choose a file';
}
echo json_encode($response);
}
function getFileName(){
$con = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME) or die('Unable to connect');
$sql = "SELECT max(id) as id FROM images";
$result = mysqli_fetch_array(mysqli_query($con,$sql));
mysqli_close($con);
if($result['id']==null){
return 1;
}else{
return ++$result['id'];
}
}
请帮帮我!