I have been working on this for hours. I cannot find the answer
online or on the chat. In a previous activity, I submit a search for
a card. This activity displays it on screen by downloading and
parsing the HTML for the image link I get the link (verified by the
log output), run it through HttpGet, get the HttpResponse, get the
InputStream, run it through BitmapFactory.decodeStream(), and assign
it to a Bitmap. When I try to set the Bitmap to the ImageView and
call invalidate(), no picture shows up. I have tried to debug this,
but with no luck. Help?
ViewCardActivity.java:
//imports and package declarations have been omitted
public class ViewCardActivity extends Activity implements Runnable {
private ViewFlipper mFlipper;
private ImageView mImageView;
private TextView mTextView;
private String mHtml;
private ProgressDialog mProgressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewcard);
mFlipper = (ViewFlipper) findViewById(R.id.flipper);
mImageView = (ImageView) findViewById(R.id.cardPicture);
mTextView = (TextView) findViewById(R.id.oracleText);
}
@Override
protected void onStart() {
super.onStart();
mProgressDialog = ProgressDialog.show(this, "",
"Downloading. Please wait...", true);
handler.post(this);
// startActivity(new Intent(Intent.ACTION_VIEW,
Uri.parse(url)));
}
@Override
public void run() {
String cardName = getIntent().getStringExtra(
"org.twilightwolf90.magic.cardname");
String url = "http://magiccards.info/autocard.php?card=" +
cardName;
HttpClient client = new DefaultHttpClient();
HttpContext context = new BasicHttpContext();
HttpGet get = new HttpGet(url);
try {
HttpResponse response = client.execute(get, context);
BufferedReader br = new BufferedReader(new
InputStreamReader(
response.getEntity().getContent()));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
mHtml = sb.toString();
int start = mHtml.indexOf("<h1>") + 13;
int end = mHtml.indexOf(">", start);
String imgUrl = mHtml.substring(start + 1, end
- 6);
if (imgUrl.charAt(0) != 'h') {
imgUrl = "http://magiccards.info/" +
imgUrl;
}
imgUrl += ".jpg";
Log.v(this.toString(), imgUrl);
get = new HttpGet(imgUrl);
response = client.execute(get, context);
Bitmap img =
BitmapFactory.decodeStream(response.getEntity()
.getContent());
mImageView.setImageBitmap(img);
mImageView.invalidate();
} catch (Exception ex) {
Log.v(this.toString(), "Second try failed");
ex.printStackTrace();
} finally {
try {
br.close();
} catch (Exception ex) {
Log.v(this.toString(), "Third try
failed");
}
}
} catch (Exception ex) {
Log.v(this.toString(), "First try failed");
}
mProgressDialog.dismiss();
handler.sendEmptyMessage(0);
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
mProgressDialog.dismiss();
}
};
}
END ViewCardActivity.java
viewcard.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ViewFlipper android:id="@+id/flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/cardPicture"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<TextView android:id="@+id/oracleText"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</ViewFlipper>
</LinearLayout>
END viewcard.xml
Any help would be appreciated. Thanks.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---