Skip to content

Instantly share code, notes, and snippets.

@koyachi
Created November 26, 2009 10:57
Show Gist options
  • Save koyachi/243396 to your computer and use it in GitHub Desktop.
Save koyachi/243396 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.buffr.Sketch20091125_01"
android:versionCode="1"
android:versionName="1.0">
<uses-permission android:name="android.permission.INTERNET" />
<application android:label="@string/app_name">
<activity android:name=".Sketch20091125_01"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
url指定した画像をダウンロードして表示
memo
- ネット接続するためにmanifestファイルいじった
<uses-permission android:name="android.permission.INTERNET" />
- InputStream一度で最後まで読んでくれないのでEOF(-1)返ってくるまで繰り返す
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="20px"
android:text="Hello World, Sketch20091125_01"
/>
<ImageView
android:id="@+id/imageview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
package org.buffr.Sketch20091125_01;
import android.app.Activity;
import android.os.Bundle;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.widget.ImageView;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import java.io.InputStream;
import android.util.Log;
public class Sketch20091125_01 extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView iv = (ImageView)findViewById(R.id.imageview);
// drawSquare(iv, 0x00b4f5);
drawJPEG(iv, "http://www.google.co.jp/intl/en_com/images/logo_plain.png");
}
public void drawSquare(ImageView iv, int color) {
int width = 100;
int height = 100;
int[] colors = new int[width * height];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
colors[y * width + x] = 0xFF000000 | color;
}
}
Bitmap bmp = Bitmap.createBitmap(colors,width,height, Bitmap.Config.ARGB_8888);
iv.setImageBitmap(bmp);
}
public void drawJPEG(ImageView iv, String url) {
HttpResponse res = download(url);
Bitmap bmp = getBitmapFromHttpResponse(res);
iv.setImageBitmap(bmp);
}
public Bitmap getBitmapFromHttpResponse(HttpResponse res) {
StatusLine status = res.getStatusLine();
HttpEntity entity = res.getEntity();
int length = (int)entity.getContentLength();
InputStream dataStream = null;
try {
dataStream = entity.getContent();
} catch (java.io.IOException e) {
Log.d("drawJPEG", "IOException get content" + e);
finish();
}
byte[] data = new byte[length];
try {
Log.d("drawJPEG", "before read");
int read_byte = 0;
int read_offset = 0;
int read_length = length;
while (read_byte != -1) {
read_byte = dataStream.read(data, read_offset, read_length);
read_offset += read_byte;
read_length -= read_byte;
Log.d("drawJPEG", "after read.... " + read_byte);
}
Log.d("drawJPEG", "after read " + read_byte);
} catch (java.io.IOException e) {
Log.d("drawJPEG", "IOException read" + e);
finish();
}
Log.d("drawJPEG", "before decodeByteArray");
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, length);
Log.d("drawJPEG", "after decodeByteArray " + ((bmp != null) ? "OK" : "NG"));
return bmp;
}
public HttpResponse download(String url) {
HttpGet req = new HttpGet(url);
req.addHeader("User-Agent", "Firefox3.5");
try {
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse res = client.execute(req);
return res;
}
catch(Exception e) {
Log.d("drawJPEG", "Exception download" + e);
finish();
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment