Android從相簿中選擇圖片顯示出來
阿新 • • 發佈:2019-02-03
下面的兩篇部落格我是選擇其中的一部分使用的。大家可以自己試試。
第一篇:http://blog.csdn.net/jackyguo1992/article/details/26729107
一、選擇圖片
定義Intent跳轉到特定相簿的Uri下挑選,然後將挑選結果返回給Activity
用到startActivityForResult
- Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
-
startActivityForResult(intent, RESULT);
二、圖片縮放
這裡講的很詳細
照它所說有3種方法:
第一種是BitmapFactory和BitmapFactory.Options,同比例縮放,但是效率高
第二種是使用Bitmap加Matrix來縮放,能夠不同比例,但是效率低
第三種是用2.2新加的類ThumbnailUtils來做,這方法不懂,好複雜。。
我用第一種,將圖片放在Imageview中
- privatevoid showPhoto(ImageView photo){
-
String picturePath = target.getInfo(Target.TargetPhotoPath);// 圖片的uri
- if(picturePath.equals(""))
- return;
- // 縮放圖片, width, height 按相同比例縮放圖片
- BitmapFactory.Options options = new BitmapFactory.Options();
- // options 設為true時,構造出的bitmap沒有圖片,只有一些長寬等配置資訊,但比較快,設為false時,才有圖片
- options.inJustDecodeBounds = true;
-
Bitmap bitmap = BitmapFactory.decodeFile(picturePath, options);
- int scale = (int)( options.outWidth / (float)300);
- if(scale <= 0)
- scale = 1;
- options.inSampleSize = scale;
- options.inJustDecodeBounds = false;
- bitmap = BitmapFactory.decodeFile(picturePath, options);
- photo.setImageBitmap(bitmap);
- photo.setMaxHeight(350);
- }
三、程式碼
- publicclass RemindActivity extends Activity {
- private ImageView photo;
- privatestaticfinalint RESULT = 1;
- Target target;
- @Override
- protectedvoid onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_remind);
- photo = (ImageView)findViewById(R.id.photo);
- target = new Target(this);
- showPhoto(photo);
- photo.setOnClickListener(new OnClickListener(){
- @Override
- publicvoid onClick(View arg0) {
- // TODO Auto-generated method stub
- Dialog dialog = new AlertDialog.Builder(RemindActivity.this)
- .setTitle("從相簿裡選擇照片").setMessage("確定要更換照片?").setPositiveButton("確定",
- new DialogInterface.OnClickListener() {
- @Override
- publicvoid onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
- startActivityForResult(intent, RESULT);
- }
- }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
- @Override
- publicvoid onClick(DialogInterface dialog, int which) {
- // TODO Auto-generated method stub
- dialog.cancel();
- }
- }).create();
- dialog.show();
- }
- });
- }
- @Override
- protectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {
- // TODO Auto-generated method stub
- super.onActivityResult(requestCode, resultCode, data);
- if(requestCode == RESULT && resultCode == RESULT_OK && data != null){
- Uri selectedImage = data.getData();
- String[] filePathColumn = { MediaStore.Images.Media.DATA };
- Cursor cursor = getContentResolver().query(selectedImage,
- filePathColumn, null, null, null);
- cursor.moveToFirst();
- int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
- String picturePath = cursor.getString(columnIndex);
- cursor.close();
- target.setInfo(Target.TargetPhotoPath, picturePath);
- showPhoto(photo);
- }
- }
- privatevoid showPhoto(ImageView photo){
- String picturePath = target.getInfo(Target.TargetPhotoPath);
- if(picturePath.equals(""))
- return;
- // 縮放圖片, width, height 按相同比例縮放圖片
- BitmapFactory.Options options = new BitmapFactory.Options();
- // options 設為true時,構造出的bitmap沒有圖片,只有一些長寬等配置資訊,但比較快,設為false時,才有圖片
- options.inJustDecodeBounds = true;
- Bitmap bitmap = BitmapFactory.decodeFile(picturePath, options);
- int scale = (int)( options.outWidth / (float)300);
- if(scale <= 0)
- scale = 1;
- options.inSampleSize = scale;
- options.inJustDecodeBounds = false;
- bitmap = BitmapFactory.decodeFile(picturePath, options);
- photo.setImageBitmap(bitmap);
- photo.setMaxHeight(350);
- }
第二篇地址:http://www.oschina.net/question/234345_40138
public class Lesson_01_Pic extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.b01);
button.setText("選擇圖片");
button.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
Intent intent = new Intent();
/* 開啟Pictures畫面Type設定為image */
intent.setType("image/*");
/* 使用Intent.ACTION_GET_CONTENT這個Action */
intent.setAction(Intent.ACTION_GET_CONTENT);
/* 取得相片後返回本畫面 */
startActivityForResult(intent, 1);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
Log.e("uri", uri.toString());
ContentResolver cr = this.getContentResolver();
try {
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
ImageView imageView = (ImageView) findViewById(R.id.iv01);
/* 將Bitmap設定到ImageView */
imageView.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
Log.e("Exception", e.getMessage(),e);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
}