2017-09-12 1 views
0

진행률 막대 및 비율로 업로드 파일의 진행률을 표시하는 방법을 찾으려고합니다. 진행률 막대를 추가하고 파일의 비율을 전송해야합니다.Android 업로드 이미지 진행률 막대

여기 내 코드가 있습니다. 파일을 전송 중이지만 진행률 표시 줄이 없습니다.

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

이 시도 http://suyu-android-dev.blogspot.in/2012/09/display-percentage-on-progressbar .html – Raghavendra

답변

1

Volley 대신 VolleyPlus를 사용하여이 기능을 추가 할 수 있습니다.

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

+0

감사합니다. 이것을 시도하고 나를 위해 작동한다면 당신을 업데이 트합니다 ... –

+0

안녕, 내가 그 코드 선생님을 적용 할 수 있습니까? Volley Plus는 잘하고 있습니다 ..하지만 jsonRequest.setOnProgressListener를 어디에 넣어야할지 모르겠군요. Pls는 힌트를 제공합니다. 고마워요, Hristo! –

+1

StringRequest 생성자 사이에 넣고이를 que에 추가해야합니다. (바로 앞에 : queue.add (stringRequest)) –