0

得るために私は何をしますか getContentLength

ここに私のコードがあります:

HttpGet request = new HttpGet(MyConstants.URL);
HttpResponse response = null;
response = httpClient.execute(request);
HttpEntity httpEntity = response.getEntity();

私は自分のコードがこのサンプルコードのように機能するようにしようとしています:

サンプルコード:

 int count;
try {
    URL url = new URL(f_url[0]);
    URLConnection conection = url.openConnection();
    conection.connect();
    // getting file length
    int lenghtOfFile = conection.getContentLength();

    // input stream to read file - with 8k buffer
    InputStream input = new BufferedInputStream(url.openStream(), 8192);

    // Output stream to write file
    OutputStream output = new FileOutputStream("/sdcard/downloadedfile.jpg");

    byte data[] = new byte[1024];

    long total = 0;

    while ((count = input.read(data)) != -1) {
        total += count;
        // publishing the progress....
        // After this onProgressUpdate will be called
        publishProgress(""+(int)((total*100)/lenghtOfFile));

        // writing data to file
        output.write(data, 0, count);
    }

    // flushing output
    output.flush();

    // closing streams
    output.close();
    input.close();

} catch (Exception e) {
    Log.e("Error: ", e.getMessage());
}
4

3 に答える 3

1

これはあなたが望むものの完全な例です..

AsyncOncImageActivity.java

public class AsyncOncImageActivity extends Activity {
/** Called when the activity is first created. */
ProgressBar pbHorizontal;
ImageView ivImage;
//Button btnIncrement;
String URL="http://appcolumn5.columnfivemedia.netdna-cdn.com/wp-content/uploads/2012/03/TakePart-Infographic_MarijuanaAttitudesandLegislation.png";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    /*** FIND ALL COMPONENT ***/
    ivImage = (ImageView) findViewById(R.id.ivImage);
    pbHorizontal=(ProgressBar) findViewById(R.id.pbHorizontal);
    //btnIncrement=(Button) findViewById(R.id.btnIncrement);

    /*** DOWNLOAD IMAG FROM URL AND READ FROM SD CARD***/
    new AsyncDemo(URL,pbHorizontal,ivImage).execute(this);
}

class AsyncDemo extends AsyncTask<Context, ProgressBar, Bitmap>
{
    String url;
    Boolean cancelTask=true;
    ProgressBar pbHorizontal;
    ImageView ivImage;
    AsyncDemo(String url,ProgressBar pbHorizontal,ImageView ivImage)
    {
        this.url = url;
        this.pbHorizontal=pbHorizontal;
        this.ivImage=ivImage;
    }

    @Override
    protected Bitmap doInBackground(Context... params) {
        // TODO Auto-generated method stub
        try {
            File root = Environment.getExternalStorageDirectory();
            URL u = new URL(url);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            FileOutputStream f = new FileOutputStream(new File(root, "c5.jpg"));

            InputStream in = c.getInputStream();
                // here u will get content length (Size of image)
            System.out.println("Content length"+c.getContentLength());
            //pbHorizontal.setMax(c.getContentLength());

            byte[] buffer = new byte[1024];
            int len1 = 0;
            /*** WRITTING IMAGE TO SD CARD **/
            while ((len1 = in.read(buffer)) > 0)// && cancelTask==true) 
            {
                f.write(buffer, 0, len1);
                System.out.println("current buf: "+len1);
                //pbHorizontal.incrementProgressBy(len1);
                pbHorizontal.setProgress(pbHorizontal.getProgress()+len1);
                //System.out.println("status Loop:"+this.getStatus());
            }
            f.close();// image stored in sd card


            /*** READING IMAGE FROM SD CARD ***/
            String imageInSD = "/sdcard/c5.jpg";
           // System.out.println("statusf inis:"+this.getStatus());
            Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);

            return bitmap;

        } catch (Exception e) {
            Log.d("Downloader", e.getMessage());
            return null;
        }
    }
    @Override
    protected void onPostExecute(Bitmap result )  
    {
        ivImage.setImageBitmap(result);
        pbHorizontal.setVisibility(ProgressBar.GONE);
    }
    protected void stopIt()
    {
        cancelTask=false;
    }
}

}

main.xml

    <ImageView
        android:id="@+id/ivImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:src="@drawable/ic_launcher" />



    <ProgressBar
        android:id="@+id/pbHorizontal"
        android:progress="0"
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="64dp" />

</RelativeLayout>
于 2012-04-06T15:31:41.017 に答える
1

httpEntity.getContentLength()?

于 2012-04-06T15:32:16.207 に答える
0
File root = Environment.getExternalStorageDirectory();
        URL u = new URL(url);
        HttpURLConnection c = (HttpURLConnection) u.openConnection();
        c.setRequestMethod("GET");
        c.setDoOutput(true);
        c.connect();

       // FileOutputStream f = new FileOutputStream(new File(root, "c5.jpg"));

        InputStream in = c.getInputStream();
            // here u will get content length (Size of image)
        System.out.println("Content length"+c.getContentLength());

このコードは、画像のサイズ(長さ)を示しますこれを試してください

于 2012-04-06T17:29:40.993 に答える