1. 程式人生 > >android---從相簿中選擇圖片

android---從相簿中選擇圖片

private ImageView picture;

public static final int CHOOSE_PHOTO=2;

picture=(ImageView)findViewById(R.id.imageView);


run_Permissions();

//從相簿選擇照片
private void run_Permissions(){
    Button Album=(Button)findViewById(R.id.album);
    Album.setOnClickListener(new View.OnClickListener() {
        @Override
public void onClick(View view) { //動態申請對SD卡讀寫的許可權 if(ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions(MainActivity.this,new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE
},1); }else { openAlbum(); } } }); } private void openAlbum(){ Intent intent=new Intent("android.intent.action.GET_CONTENT"); intent.setType("image/*"); startActivityForResult(intent,CHOOSE_PHOTO); //開啟相簿 } @Override public void onRequestPermissionsResult(int
requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode){ case 1: if(grantResults.length>0 &&grantResults[0] == PackageManager.PERMISSION_GRANTED){ openAlbum(); }else { Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT).show(); } break; default: } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode){ case CHOOSE_PHOTO: handleImageOnKitKat(data); } } private void handleImageOnKitKat(Intent data){ //處理圖片 String imagePath = null; Uri uri=data.getData(); if (DocumentsContract.isDocumentUri(this, uri)) { //如果是document型別的Uri,則通過document id處理 String docId = DocumentsContract.getDocumentId(uri); if ("com.android.providers.media.documents".equals(uri.getAuthority())) { String id = docId.split(":")[1]; //解析出數字格式的id String selection = MediaStore.Images.Media._ID + "=" + id; imagePath = getImagePath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection); } else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) { Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId)); imagePath = getImagePath(contentUri, null); } } else if ("content".equalsIgnoreCase(uri.getScheme())) { //如果是cntent型別的Uri,則使用普通方式處理 imagePath = getImagePath(uri, null); } else if ("file".equalsIgnoreCase(uri.getScheme())) { //如果是file型別的Uri,直接獲取圖片路徑即可 imagePath = uri.getPath(); } displayImage(imagePath); } private String getImagePath(Uri uri,String selection){ //通過Uri和selection來獲取真實的圖片路徑 String path=null; Cursor cursor=getContentResolver().query(uri,null,selection,null,null); if(cursor != null){ if(cursor.moveToFirst()){ path=cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); } cursor.close(); } return path; } private void displayImage(String imagePath){ //顯示圖片 if(imagePath != null){ Bitmap bitmap= BitmapFactory.decodeFile(imagePath); picture.setImageBitmap(bitmap); }else { Toast.makeText(this, "failed to get image", Toast.LENGTH_SHORT).show(); } }
佈局就一個按鈕Button,一個ImageView。 程式碼註釋很清楚。
參考自第一行程式碼