Sunday, December 28, 2014

Android AsyncTask onProgressUpdate with ProgressDialog - How to get progress percentage of an uploading file?

private class ImageUpload extends AsyncTask<String,Integer,String> {

        @Override
        protected void onProgressUpdate(Integer... progress) {
            // TODO Auto-generated method stub

            mProgressDialog.setProgress((int) (progress[0]));
        }

        @Override
        protected String doInBackground( String... params ) {


            String result = "";


            HttpClient httpClient = new DefaultHttpClient();


            HttpPost httpPostRequest = new HttpPost( "http://192.168.212.177:8089/services/imageUpload" );

            try {
                Date todaysdate = new Date();
                for ( String imageFile : params ) {
                    Bitmap bm = BitmapFactory.decodeFile(imageFile);
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
                    bm.compress(Bitmap.CompressFormat.JPEG, 15, baos); //bm is the bitmap object   
                    byte[] b = baos.toByteArray(); 
                    String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);

                    FileBody bin = new FileBody( file );

                    MultipartEntityBuilder multiPartEntityBuilder = MultipartEntityBuilder.create();

                    int strIndex = imageFile.lastIndexOf( "/" );
                    String imageName = imageFile.substring( strIndex + 1 , imageFile.length() );

                    multiPartEntityBuilder.addPart( "imageFile" , new StringBody(encodedImage , ContentType.TEXT_PLAIN ) );
                    multiPartEntityBuilder.addPart( "filename" , new StringBody( farmersEnteredNumber + "_" + datemill.getTime() , ContentType.TEXT_PLAIN ) );
                    multiPartEntityBuilder.addPart( "imageName" , new StringBody( imageName, ContentType.TEXT_PLAIN ) );


                    long total = 0;

                    long lenghtOfFile = multiPartEntityBuilder.build().getContentLength();
                    publishProgress((int)((total*100)/lenghtOfFile));
                    httpPostRequest.setEntity( multiPartEntityBuilder.build() );


                    HttpResponse httpResponse = null;
                    httpResponse = httpClient.execute( httpPostRequest );


                    InputStream inputStream = null;
                    inputStream = httpResponse.getEntity().getContent();

                    if ( inputStream != null )
                        result = convertInputStreamToString( inputStream );
                    else
                        result = "Did not work!";
                    return result;
                }
            } catch ( Exception e ) {

                return null;
            }

            return result;
        }

        @Override
        protected void onPreExecute() {

            // TODO Auto-generated method stub
            super.onPreExecute();
            FarmerDetails.this.runOnUiThread( new Runnable() {

                public void run() {

                    mProgressDialog = new ProgressDialog( FarmerDetails.this );
                    mProgressDialog.setTitle( "Image Uploading" );
                    mProgressDialog.setMessage( "Uploading Image Please Wait" );
                    mProgressDialog.setIndeterminate( false );
                    mProgressDialog.setMax(100);
                    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
                    mProgressDialog.setCancelable(true);
                    mProgressDialog.show();

                }
            } );



        }

        @Override
        protected void onPostExecute( String result ) {
            super.onPostExecute( result );
            mProgressDialog.dismiss();
            // TODO Auto-generated method stub


        }

    }

Android how to create triangle and rectangle shape programatically?

triangle.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item >
        <rotate
            android:fromDegrees="45"
            android:toDegrees="45"
            android:pivotX="-40%"
            android:pivotY="87%" >
            <shape
                android:shape="rectangle"  >
                <stroke android:color="@android:color/transparent" android:width="10dp"/>
                <solid
                    android:color="#000000"  />
            </shape>
        </rotate>
    </item>
</layer-list>
main.xml
<RelativeLayout
        android:id="@+id/rlv2"
        android:layout_width="50dp"
        android:layout_height="50dp"  
        android:layout_below="@+id/rlv1"
        android:background="@drawable/triangle"
        android:rotation="180" />
result:

It is possible to create this simple shape as tag picture in Android code behind

Shape shape = new Shape() {
    Path path = new Path();
    @Override
    protected void onResize(float width, float height) {
        path.reset();
        path.lineTo(width - height / 2, 0);
        path.lineTo(width, height / 2);
        path.lineTo(width - height / 2, height);
        path.lineTo(0, height);
        path.close();
    }
    @Override
    public void draw(Canvas canvas, Paint paint) {
        canvas.drawPath(path, paint);
    }
};
ShapeDrawable d = new ShapeDrawable(shape);
d.getPaint().setColor(0xff6699bb);

someView.setBackgroundDrawable(d);
result: