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:
class PostAsync extends AsyncTask<String, String, Integer> {
private ProgressDialog pDialog;
private static final String sourcepath = "http://www.planwallpaper.com/static/images/supranatural-3d-wallpaper-images.jpg";
@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Attempting download...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Integer doInBackground(String... args) {
int fileSize = 0;
try {
URL url = new URL(sourcepath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
fileSize = connection.getContentLength();
InputStream inputStream = connection.getInputStream();
bmp = BitmapFactory.decodeStream(inputStream);
inputStream.close();
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return fileSize;
}
protected void onPostExecute(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:
public class MainActivity extends ActionBarActivity {
private ImageView image;
private Bitmap bmp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.imageView);
new PostAsync().execute();
}
@Override
protected void onResume() {
super.onResume();
//put on resume functionality here....
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(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.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
class PostAsync extends AsyncTask<String, String, Integer> {
private ProgressDialog pDialog;
private static final String sourcepath = "http://www.planwallpaper.com/static/images/supranatural-3d-wallpaper-images.jpg";
@Override
protected void onPreExecute() {
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Attempting download...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected Integer doInBackground(String... args) {
int fileSize = 0;
try {
URL url = new URL(sourcepath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
fileSize = connection.getContentLength();
InputStream inputStream = connection.getInputStream();
bmp = BitmapFactory.decodeStream(inputStream);
inputStream.close();
connection.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return fileSize;
}
protected void onPostExecute(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>