Android Httpurlconnection Getcontentlength() Return Wrong Result
in my application i'm wrote simole download manager to download files via internet. but HttpURLConnection getContentLength() return wrong result such as -1, i'm testing this image
Solution 1:
It looks like the main problem in your code is the call to connection.setDoOutput(true);
.
With that call included, I got a return value of -1
from getContentLength(), and it also didn't download the image successfully.
When I removed the call to connection.setDoOutput(true);
it started to work just fine and return valid values, and also successfully download the image.
Here is the code that worked for me, I added code to show a toast with the return value of getContentLength()
after the download has completed:
classPostAsyncextendsAsyncTask<String, String, Integer> {
private ProgressDialog pDialog;
privatestaticfinalStringsourcepath="http://www.planwallpaper.com/static/images/supranatural-3d-wallpaper-images.jpg";
@OverrideprotectedvoidonPreExecute() {
pDialog = newProgressDialog(MainActivity.this);
pDialog.setMessage("Attempting download...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Overrideprotected Integer doInBackground(String... args) {
intfileSize=0;
try {
URLurl=newURL(sourcepath);
HttpURLConnectionconnection= (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
fileSize = connection.getContentLength();
InputStreaminputStream= connection.getInputStream();
bmp = BitmapFactory.decodeStream(inputStream);
inputStream.close();
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return fileSize;
}
protectedvoidonPostExecute(Integer fileSize) {
pDialog.dismiss();
MainActivity.this.image.setImageBitmap(bmp);
Toast.makeText(MainActivity.this, "file size: " + fileSize, Toast.LENGTH_LONG).show();
}
}
I tested with two different images just to make sure that it returned different values.
First, with the image in your question:
And with a different image:
Full MainActivity class:
publicclassMainActivityextendsActionBarActivity {
private ImageView image;
private Bitmap bmp;
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.imageView);
newPostAsync().execute();
}
@OverrideprotectedvoidonResume() {
super.onResume();
//put on resume functionality here....
}
@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
returntrue;
}
@OverridepublicbooleanonOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.intid= item.getItemId();
//noinspection SimplifiableIfStatementif (id == R.id.action_settings) {
returntrue;
}
returnsuper.onOptionsItemSelected(item);
}
classPostAsyncextendsAsyncTask<String, String, Integer> {
private ProgressDialog pDialog;
privatestaticfinalStringsourcepath="http://www.planwallpaper.com/static/images/supranatural-3d-wallpaper-images.jpg";
@OverrideprotectedvoidonPreExecute() {
pDialog = newProgressDialog(MainActivity.this);
pDialog.setMessage("Attempting download...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Overrideprotected Integer doInBackground(String... args) {
intfileSize=0;
try {
URLurl=newURL(sourcepath);
HttpURLConnectionconnection= (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
fileSize = connection.getContentLength();
InputStreaminputStream= connection.getInputStream();
bmp = BitmapFactory.decodeStream(inputStream);
inputStream.close();
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return fileSize;
}
protectedvoidonPostExecute(Integer fileSize) {
pDialog.dismiss();
MainActivity.this.image.setImageBitmap(bmp);
Toast.makeText(MainActivity.this, "file size: " + fileSize, Toast.LENGTH_LONG).show();
}
}
}
activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<ImageView android:id="@+id/imageView" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
Post a Comment for "Android Httpurlconnection Getcontentlength() Return Wrong Result"