Basic認証とストレージに画像保存とイメージビューに画像を保存まで。
package com.taku.nfchome; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.URL; import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Date; import java.util.List; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.auth.AuthScope; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpHead; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.BufferedHttpEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.util.EntityUtils; import extra.UrlManager; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Bundle; import android.os.Environment; import android.util.Log; import android.widget.ImageView; public class ImageActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.image_activity); final ImageView iv = (ImageView)findViewById(R.id.imageView); new AsyncTask() { @Override protected Bitmap doInBackground(String... params) { Bitmap bmp = null; try{ DefaultHttpClient httpClient = new DefaultHttpClient(); String url = new String(params[0]); HttpGet request = new HttpGet(url); // 認証するUserとPW UsernamePasswordCredentials credentials = new UsernamePasswordCredentials("「ユーザ名」","「パスワード」"); AuthScope scope = new AuthScope(request.getURI().getHost(), request.getURI().getPort()); httpClient.getCredentialsProvider().setCredentials(scope, credentials); HttpResponse httpResponse = httpClient.execute(request); // ステータスコードを取得 int statusCode = httpResponse.getStatusLine().getStatusCode(); HttpGet httpRequest = new HttpGet(url); //ーーー画像生成処理ーーー HttpResponse response = (HttpResponse) httpClient.execute(httpRequest); HttpEntity entity1 = response.getEntity(); BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity1); InputStream is = bufHttpEntity.getContent(); bmp = getBitmap(is); // リソースを解放 entity1.consumeContent(); // クライアントを終了させる httpClient.getConnectionManager().shutdown(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return bmp; } @Override protected void onPostExecute(Bitmap bmp) { Log.v("image", "ok"); iv.setImageBitmap(bmp); saveBitmapToSd(bmp); } }.execute("「URL」"); } /** * 画像に変換 * @param is * @return */ public static Bitmap getBitmap(InputStream is) { Bitmap bmp = null; byte[] line = new byte[1024]; int byteSize = 0; try { // バイト単位での読込 ByteArrayOutputStream out = new ByteArrayOutputStream(); while ((byteSize = is.read(line)) > 0) { out.write(line, 0, byteSize); } // オプション BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 0; byte[] byteArray = out.toByteArray(); Log.v("image", "byteArray.length = " + byteArray.length); bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length, options); is.close(); } catch (Exception e) { e.printStackTrace(); } return bmp; } /** * 画像をストレージへ保存 * @param mBitmap */ public void saveBitmapToSd(Bitmap mBitmap) { try { // sdcardフォルダを指定 File root = Environment.getExternalStorageDirectory(); // 日付でファイル名を作成 Date mDate = new Date(); SimpleDateFormat fileName = new SimpleDateFormat("yyyyMMdd_HHmmss"); // 保存処理開始 FileOutputStream fos = null; fos = new FileOutputStream(new File(root, fileName.format(mDate) + ".jpg")); Log.v("image", (new File(root, fileName.format(mDate) + ".jpg").toString())); // jpegで保存 mBitmap.compress(CompressFormat.JPEG, 100, fos); // 保存処理終了 fos.close(); } catch (Exception e) { Log.e("Error", "" + e.toString()); } } }