Skip to content Skip to sidebar Skip to footer

Android Jpg Animation

I would like to download 10 JPG pictures from a site. The pictures where taken at intervals >> that is no problem How do I save pictures in an array of pictures. How would I

Solution 1:

You'll probably want to use AnimationDrawable in an ImageView.

Add frames using the addFrame(Drawable frame, int duration) method, and start the animation using start().

You can use many different methods to download and store the image (perhaps to external storage, or a db).

Solution 2:

I have an example of this, here I am downloading three images form site and then store in it path "/mnt/sdcard/..". If you want to show this in animation then I think that you can use flipper and then add this image dynamically and using an async task you can flip image next.

import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

publicclassTestActivityextendsActivity {
    /** Called when the activity is first created. */
    Button btn;
    @OverridepublicvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn=(Button)findViewById(R.id.startBtn);
        finalStringurl1="http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg";
        finalStringurl2="http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg";
        finalStringurl3="http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg";
        btn.setOnClickListener(newOnClickListener() {
            @OverridepublicvoidonClick(View v) {
                newAsyncDownload().execute(url1,url2,url3);
            }
        });
    }

    //------------------------------Class AsyncDownload----------------------------------publicclassAsyncDownloadextendsAsyncTask<String, String, String>
    {
        ProgressDialog dialog;

        @OverrideprotectedvoidonPostExecute(String result) {
            // TODO Auto-generated method stubsuper.onPostExecute(result);    
            dialog.dismiss();
            Toast.makeText(TestActivity.this,"Downloading complate successfully",2).show();
        }

        @OverrideprotectedvoidonPreExecute() {
            // TODO Auto-generated method stubsuper.onPreExecute();
            dialog=newProgressDialog(TestActivity.this);
            dialog.setMessage("Downloading file..");
            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            dialog.setMax(100);
            dialog.show();
        }

        @OverrideprotectedvoidonProgressUpdate(String... values) {
            // TODO Auto-generated method stubsuper.onProgressUpdate(values);
            dialog.setProgress(Integer.parseInt(values[0]));
        }

        @Overrideprotected String doInBackground(String... aurl) {
            int count;
            int lenghtOfFile=0;
            longtotal=0;
            try {
                for(int i=0;i<aurl.length;i++)
                {
                    URLurl=newURL(aurl[i]);
                    URLConnectionconexion= url.openConnection();
                    conexion.connect();
                    lenghtOfFile =lenghtOfFile+conexion.getContentLength();
                }
                for(int i=0;i<aurl.length;i++)
                {
                    URLurl=newURL(aurl[i]);
                    URLConnectionconexion= url.openConnection();
                    conexion.connect();
                    InputStreaminput=newBufferedInputStream(url.openStream());
                    OutputStreamoutput=newFileOutputStream("/mnt/sdcard/img"+i+".jpg");

                    byte data[] = newbyte[1024];               

                    while ((count = input.read(data)) != -1) {
                        total += count;
                        publishProgress(""+(int)((total*100)/lenghtOfFile));
                        output.write(data, 0, count);
                    }

                    output.flush();
                    output.close();
                    input.close();
                }
            } catch (Exception e) {
                e.getMessage();

            }
            returnnull;
        }

    }

}

Post a Comment for "Android Jpg Animation"