トップ «前の日記(2013-06-16) 最新 次の日記(2013-06-23)» 編集

Cocoa練習帳

iOS/iPhone/iPad/watchOS/tvOS/MacOSX/Android プログラミング, Objective-C, Cocoa, Swiftなど

2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|10|11|12|
2017|01|02|03|04|05|06|07|08|09|10|11|12|
2018|01|02|03|04|05|06|07|08|09|10|11|12|
2019|01|02|03|04|05|06|07|08|09|10|11|12|
2020|01|02|03|04|05|06|07|08|09|10|11|12|
2021|01|02|03|04|05|06|07|08|09|10|11|12|
2022|01|02|03|04|05|06|07|08|09|10|11|12|
2023|01|02|03|04|05|06|07|08|09|10|11|12|
2024|01|02|

2013-06-17 [Android]ネット上の画像の全画面表示

AndroidManifest.xmlで、通信の利用を有効にする。

<uses-permission android:name="android.permission.INTERNET" />

例では、主アクティビティで、画像を一個、表示するようにしている。

<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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
	
    <ImageView
        android:id="@+id/imageview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitStart" />
	
</RelativeLayout>

activity_vertical_marginとactivity_horizontal_marginは0に設定している。scaleTypeはfitStartを指定しているので、画面上部に表示される。

AsyncTaskを使って、画像をダンロードして、表示した。

public class MainActivity extends Activity {
	private	ImageView	imageView = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		imageView = (ImageView)findViewById(R.id.imageview);
		new DownloadImageTask().execute("画像のURL");
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	private class DownloadImageTask extends AsyncTask {
		private static final String TAG = "DownloadImageTask";
		
		@Override
		protected Bitmap doInBackground(String ... urlString) {
			Bitmap bitmap = null;
		    try {
		        URL url = new URL(urlString[0]);
		        URLConnection urlConnection = url.openConnection();
		        urlConnection.connect();
		        InputStream is = urlConnection.getInputStream();
		        BufferedInputStream bis = new BufferedInputStream(is);
		        bitmap = BitmapFactory.decodeStream(bis);
		        bis.close();
		        is.close();
		    } catch (IOException e) {
		        Log.e(TAG,"Error getting the image from server : " + e.getMessage().toString());
		    } 
		    return bitmap;
		}
		
		@Override
		protected void onPostExecute(Bitmap bitmap) {
			imageView.setImageBitmap(bitmap);
		}
	}
}

どうしても、気持ち悪いので、インスタンスの初期値としてnullを設定してしまう。

AsyncTaskは、全体で一度に一個の処理しか実行しないので、何でもかんでもAsyncTaskで非同期処理すると、著者のように困った事象に遭遇してしまうので気をつけた方がいい。

_ 【Cocoa練習帳】

http://www.bitz.co.jp/weblog/
http://ameblo.jp/bitz/(ミラー・サイト)

トップ «前の日記(2013-06-16) 最新 次の日記(2013-06-23)» 編集