我正在尝试找到一种方法来显示上传文件在进度条和百分比中的进度。我需要添加进度条并传输文件的百分比。
这是我的代码。它传输文件但没有进度条。
package com.example.christian.progressbar5;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
ImageView ivImage;
Button btnUpload;
ProgressBar progressBar;
EditText etFilename;
final int GALLERY_REQUEST = 38473;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etFilename = (EditText) findViewById(R.id.etFilename);
ivImage = (ImageView) findViewById(R.id.ivImage);
btnUpload = (Button) findViewById(R.id.btnUpload);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
ivImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, GALLERY_REQUEST);
}
});
btnUpload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
uploadImage();
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK) {
if(requestCode == GALLERY_REQUEST){
Uri uri = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
ivImage.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private void uploadImage()
{
final String filename = etFilename.getText().toString();
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
String URL = "http://inventsystem.esy.es/uploadimage2.php";
Bitmap resized = getResizedBitmap(bitmap, 500);
final String photo = ImageToString(resized);
if(bitmap == null || bitmap.equals("")){
Toast.makeText(this, "No Image", Toast.LENGTH_SHORT).show();
return;
}
if(filename.equals("")){
Toast.makeText(this, "What's the filename?", Toast.LENGTH_SHORT).show();
return;
}
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Toast.makeText(MainActivity.this, "Image has been Uploaded", Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Something went wrong...", Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("image", photo);
params.put("name", filename);
return params;
}
};
queue.add(stringRequest);
}
private String ImageToString(Bitmap bitmap)
{
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,100,byteArray);
byte[] imgBytes = byteArray.toByteArray();
return Base64.encodeToString(imgBytes,Base64.DEFAULT);
}
public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();
float bitmapRatio = (float)width / (float) height;
if (bitmapRatio > 1) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}
}
答案 0 :(得分:1)
您可以使用VolleyPlus而不是Volley来添加此功能。
jsonRequest.setOnProgressListener(new Response.ProgressListener() {
@Override
public void onProgress(long transferredBytes, long totalSize) {
int percentage = (int) ((transferredBytes / ((float) totalSize)) * 100);
}
});
有一个很好的代码snipplet如何在这里做: https://github.com/DWorkS/VolleyPlus/issues/53