Android之从相册访问照片
从上一篇博客的基础上修改:地址
首先是activitymain.xml:
<LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/take_photo" android:text="拍照" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <Button android:id="@+id/choose_from_album" android:text="Choosefromalbum" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/picture" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>然后修改MainActivity
public static final int chose_puhoto=2;
Button chose_from_album=(Button)findViewById(R.id.choose_from_album); chose_from_album.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(MainActivity.this,new String[]{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,chose_puhoto);//打开相册 }
@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,"你拒绝了允许",Toast.LENGTH_LONG).show(); } break; default: }
}
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode){ case TAKE_PHOTP: if(resultCode==RESULT_OK){ try { Bitmap bitmap= BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); imageView.setImageBitmap(bitmap); } catch (FileNotFoundException e) { e.printStackTrace(); } } break; case chose_puhoto: if(resultCode==RESULT_OK){ if(Build.VERSION.SDK_INT>=19){ handleImageOnKitKat(data); } else{ handleImageBeforeKitKate(data); } } break; default: break; } }
private void handleImageOnKitKat(Intent data){ String imagePath=null; Uri uri=data.getData(); if(DocumentsContract.isDocumentUri(MainActivity.this,uri)){ String docId=DocumentsContract.getDocumentId(uri); if("com.android.providers.media.documents".equals(uri.getAuthority())){ String id=docId.split(":")[1]; 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())){ imagePath=getImagePath(uri,null); } else if("file".equalsIgnoreCase(uri.getScheme())){ imagePath=uri.getPath(); } displayImage(imagePath); } private void handleImageBeforeKitKate(Intent data){ Uri uri=data.getData(); String imagePath=getImagePath(uri,null); displayImage(imagePath); }
private String getImagePath(Uri uri,String 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); imageView.setImageBitmap(bitmap); } else{ Toast.makeText(this,"failed to get Image",Toast.LENGTH_LONG).show(); } }