Created
July 10, 2012 14:04
-
-
Save sakurabird/3083429 to your computer and use it in GitHub Desktop.
イメージを選択する。Intent.ACTION_PICKとIntent.ACTION_GET_CONTENTの違いを比較する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
このプログラムを動かすにはandroid-support-v4.jarが必要 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.sakurafish.android.example.myexamplechooseimage" | |
android:versionCode="1" | |
android:versionName="1.0" > | |
<uses-sdk android:minSdkVersion="7" /> | |
<application | |
android:icon="@drawable/ic_launcher" | |
android:label="@string/app_name" > | |
<activity | |
android:name=".MainActivity" | |
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:orientation="vertical" > | |
<Button | |
android:id="@+id/btn_choose" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" | |
android:text="@string/btn_choose" | |
android:onClick="onClickChoose" /> | |
<Button | |
android:id="@+id/btn_choose2" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center" | |
android:text="@string/btn_choose2" | |
android:onClick="onClickChoose2" /> | |
<TextView | |
android:id="@+id/txt_intent" | |
android:layout_width="fill_parent" | |
android:layout_height="wrap_content" | |
/> | |
<ImageView | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:layout_gravity="center" | |
android:scaleType="centerInside" | |
android:background="#ffffffff" | |
android:id="@+id/img_picture" | |
android:src="@drawable/ic_launcher"/> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.sakurafish.android.example.myexamplechooseimage; | |
import android.content.Intent; | |
import android.graphics.Bitmap; | |
import android.graphics.BitmapFactory; | |
import android.os.Bundle; | |
import android.support.v4.app.FragmentActivity; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.ImageView; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
import java.io.FileNotFoundException; | |
import java.io.IOException; | |
import java.io.InputStream; | |
public class MainActivity extends FragmentActivity { | |
private static final int REQUEST_PICK_CONTACT = 10; | |
private static final int REQUEST_PICK_CONTACT2 = 11; | |
private TextView mTextView; | |
private ImageView mImageView; | |
private Bitmap mBitmap = null; | |
// private ProgressDialog progressDialog=null; | |
/** Called when the activity is first created. */ | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
mTextView = (TextView) findViewById(R.id.txt_intent); | |
mImageView = (ImageView) findViewById(R.id.img_picture); | |
} | |
public void onClickChoose(View view) { | |
Intent intent = new Intent(); | |
intent.setType("image/*"); | |
intent.setAction(Intent.ACTION_PICK); | |
startActivityForResult(Intent.createChooser(intent, getText(R.string.tit_choose)), | |
REQUEST_PICK_CONTACT); | |
} | |
public void onClickChoose2(View view) { | |
// access to SD card | |
Intent intent = new Intent(); | |
intent.setType("image/*"); | |
intent.setAction(Intent.ACTION_GET_CONTENT); | |
startActivityForResult(Intent.createChooser(intent, getText(R.string.tit_choose)), | |
REQUEST_PICK_CONTACT2); | |
} | |
@Override | |
public void onActivityResult(int requestCode, int resultCode, Intent data) { | |
if (resultCode == RESULT_OK) { | |
Log.i("RequestCode", String.valueOf(requestCode)); | |
Log.i("ResultCode", String.valueOf(resultCode)); | |
} else { | |
// エラー | |
Toast.makeText(this, "インテント呼び出し失敗", Toast.LENGTH_LONG).show(); | |
return; | |
} | |
switch (requestCode) { | |
case REQUEST_PICK_CONTACT: | |
// break; | |
case REQUEST_PICK_CONTACT2: | |
// break; | |
default: | |
Log.i("data", data.toString()); | |
// 選択された画像を取得 | |
try { | |
mBitmap = null; | |
mBitmap = loadBitmap(getContentResolver() | |
.openInputStream(data.getData())); | |
} catch (FileNotFoundException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
if (mBitmap != null) { | |
mTextView.setText(data.toString()); | |
mImageView.setImageBitmap(mBitmap); | |
// mBitmap.recycle(); | |
} else { | |
Toast.makeText(this, "画像取得に失敗しました", Toast.LENGTH_SHORT).show(); | |
mTextView.setText(""); | |
mImageView.setImageBitmap(null); | |
} | |
break; | |
} | |
} | |
// ファイル等のInputStreamから画像(撮影した写真)を読み込む | |
private Bitmap loadBitmap(InputStream is) { | |
// 読み込む画像のデータサイズの上限(1MB) | |
final int BITMAP_SIZE_MAX = 1 * 1024 * 1024; | |
// 読み込んだ画像を格納するBitmapを用意する | |
Bitmap bitmap = null; | |
try { | |
// ファイルサイズ(InputStreamのサイズ)が、読み込む上限を超えている場合、 | |
if (is.available() > BITMAP_SIZE_MAX) { | |
// 画像を小さくして読み込む(大きな画像を読み込んだ場合に発生するOut Of Memoryへの対策) | |
BitmapFactory.Options options = new BitmapFactory.Options(); | |
options.inSampleSize = is.available() / BITMAP_SIZE_MAX + 1; | |
bitmap = BitmapFactory.decodeStream(is, null, options); | |
// 上限を超えていない場合、 | |
} else { | |
// そのまま読み込む | |
bitmap = BitmapFactory.decodeStream(is); | |
} | |
is.close(); | |
} catch (IOException e) { | |
Toast.makeText(this, "画像取得に失敗しました", Toast.LENGTH_SHORT).show(); | |
e.printStackTrace(); | |
} | |
// 読み込んだ画像を返す | |
return bitmap; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<string name="app_name">MyExampleChooseImage</string> | |
<string name="btn_choose">select image(ACTION_PICK)</string> | |
<string name="btn_choose2">select image(ACTION_GET_CONTENT)</string> | |
<string name="tit_choose">select image</string> | |
<string name="sd_access">sd access</string> | |
<string name="mesg_data_accessing">data accessing,,,</string> | |
</resources> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment