欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android 調(diào)用系統(tǒng)相冊選擇照片

 更新時(shí)間:2020年12月09日 14:56:31   作者:一只修仙的猿  
這篇文章主要介紹了Android 調(diào)用系統(tǒng)相冊選擇照片的方法,幫助大家更好的進(jìn)行Android開發(fā),感興趣的朋友可以了解下

前言

在相冊里選擇圖片上傳也是很常見的功能了例如微信朋友圈等等。但是他們是自定義的選擇器,可以選擇多張圖片并修改。這里我們講一個(gè)最簡單的:調(diào)用系統(tǒng)的相冊選擇一張圖片并展示。另外有的讀者還想到要通過相機(jī)拍照來選擇圖片的功能,也可以參考一下我的另一篇文章Android使用系統(tǒng)相機(jī)進(jìn)行拍照

使用步驟

這里我是通過一個(gè)簡單的demo來講解怎么去實(shí)現(xiàn)這個(gè)功能。首先看布局:

 <Button
  android:id="@+id/button2"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginTop="5dp"
  android:layout_marginEnd="52dp"
  android:layout_marginRight="52dp"
  android:text="choose"
  app:layout_constraintEnd_toEndOf="parent"
  app:layout_constraintTop_toTopOf="parent" />

 <ImageView
  android:id="@+id/imageView"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:layout_marginTop="29dp"
  app:layout_constraintEnd_toEndOf="parent"
  app:layout_constraintStart_toStartOf="parent"
  app:layout_constraintTop_toBottomOf="@+id/button"
  app:srcCompat="@mipmap/ic_launcher_round" />

很簡單,就是一個(gè)按鈕和一個(gè)imageView。然后接下來讓我們想想這個(gè)功能怎么去實(shí)現(xiàn):

首先打開相冊,那么肯定要通過隱式啟動相冊activity;然后相冊返回一個(gè)路徑,我們就拿這個(gè)路徑把路徑上對應(yīng)的照片展示出來。思路挺簡單的,讓我們寫寫看:
首先看代碼:

 private Uri imageUri;
 private ImageView imageView;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  imageView = findViewById(R.id.imageView);
  Button button1 = findViewById(R.id.button2);
  button1.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
   //動態(tài)申請權(quán)限
    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{
    //執(zhí)行啟動相冊的方法
     openAlbum();
    }
   }
  });
  }
//獲取權(quán)限的結(jié)果
@Override
 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  if (requestCode == 1){
   if (grantResults.length>0&&grantResults[0] == PackageManager.PERMISSION_GRANTED) openAlbum();
   else Toast.makeText(MainActivity.this,"你拒絕了",Toast.LENGTH_SHORT).show();
  }
 }

//啟動相冊的方法
private void openAlbum(){
  Intent intent = new Intent("android.intent.action.GET_CONTENT");
  intent.setType("image/*");
  startActivityForResult(intent,2);
 }

這里先初始化控件,然后動態(tài)申請權(quán)限,因?yàn)槲覀円x取照片肯定是要讀取內(nèi)存的權(quán)限,記得在AndroidManifest中要寫明權(quán)限:

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

獲取權(quán)限后就打開相冊選擇。相冊對應(yīng)的action是android.intent.action.GET_CONTENT,setType(“image/*”)這個(gè)方法表示把所有照片顯示出來,然后開啟活動。啟動活動選擇完照片后就會返回一個(gè)intent到onActivityResult方法中,所以接下來的主要工作就是如果獲取到返回的路徑。

我們知道在安卓4.4以后是不能把文件的真實(shí)路徑直接給別的應(yīng)用的,所以返回的uri是經(jīng)過封裝的,所以我們要進(jìn)行解析取出里面的路徑。所以這里我們要進(jìn)行判斷安卓版本來進(jìn)行不同的邏輯,先看代碼:

@Override
 protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
 if (requestCode == 2){
 //判斷安卓版本
    if (resultCode == RESULT_OK&&data!=null){
    if (Build.VERSION.SDK_INT>=19)
    handImage(data);
    else handImageLow(data);
   }
  }
 }

//安卓版本大于4.4的處理方法
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
 private void handImage(Intent data){
  String path =null;
  Uri uri = data.getData();
  //根據(jù)不同的uri進(jìn)行不同的解析
  if (DocumentsContract.isDocumentUri(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;
    path = 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));
    path = getImagePath(contentUri,null);
   }
  }else if ("content".equalsIgnoreCase(uri.getScheme())){
   path = getImagePath(uri,null);
  }else if ("file".equalsIgnoreCase(uri.getScheme())){
   path = uri.getPath();
  }
  //展示圖片
  displayImage(path);
 }


//安卓小于4.4的處理方法
private void handImageLow(Intent data){
  Uri uri = data.getData();
  String path = getImagePath(uri,null);
  displayImage(path);
 }

//content類型的uri獲取圖片路徑的方法
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;
 }

//根據(jù)路徑展示圖片的方法
private void displayImage(String imagePath){
  if (imagePath != null){
   Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
   imageView.setImageBitmap(bitmap);
  }else{
   Toast.makeText(this,"fail to set image",Toast.LENGTH_SHORT).show();
  }
 }

上面的代碼很多但是不要慌,咱們一個(gè)一個(gè)來,不難理解的。首先我們知道不同的版本有兩個(gè)不同的方法來展示圖片,就是:handImage和handImageLow。content類型的uri通過getImagePath這個(gè)方法來獲取真實(shí)路徑,真實(shí)路徑通過displayImage這個(gè)方法就可以展示出來了。所以主要的工作就是怎么拿到真實(shí)路徑?,F(xiàn)在思路清晰了,讓我們一個(gè)個(gè)來看:

首先來看一下兩個(gè)工具方法:getImagePath和displayImage。

  • getImagePath學(xué)過內(nèi)容提供器會知道這個(gè)就是通過內(nèi)容提供器來獲取數(shù)據(jù)。通過這個(gè)uri以及selection獲取到一個(gè)Cursor對象。Cursor是什么呢?不了解的讀者可以查看這篇博客Android中的Cursor。然后通過這個(gè)Cursor對象的MediaStore.Images.Media.DATA這個(gè)參數(shù)就可以獲取到真實(shí)路徑了。
  • displayImage這個(gè)方法收一個(gè)真實(shí)路徑字符串,直接通過BitmapFactory.decodeFile這個(gè)方法獲取到Bitmap再顯示出來就行了

了解了工具方法后,我們的目的就很明確啦:content類型的uri或者真實(shí)路徑的String。
首先是版本低于4.4的,因?yàn)榉祷氐氖钦鎸?shí)的uri,也就是content開頭的那個(gè),所以直接通過getImagePath獲取真實(shí)路徑再通過displayImage展示即可。

接下來這個(gè)可能看起來有點(diǎn)頭疼,因?yàn)橐馕霾煌愋偷腢ri。我們一個(gè)個(gè)來看:

  • 第一種是document類型的uri。至于什么是document類型的uri這里就不深入了,只要知道有這種類型的uri,要怎么處理就好了。首先我們要獲取一個(gè)DocumentId,然后再分兩種情況處理:

第一種的是media格式的,然后我們要取出后半截字符串我們才能獲取到真正的id,這里就真正的id指的是對應(yīng)數(shù)據(jù)庫表中的id,用于selection的。MediaStore.Images.Media.EXTERNAL_CONTENT_URI就是這個(gè)照片的content類型uri,再把selection放進(jìn)去即可。
第二種通過ContentUris.withAppendedId這個(gè)方法即可獲取到content類型的uri,這個(gè)方法負(fù)責(zé)把id和contentUri連接成一個(gè)新的Uri。這個(gè)方法在這里也不詳細(xì)講解。

  • 第二種的是content類型的,那不用說直接用就行了
  • 第三種的是file類型的,這個(gè)就是真實(shí)路徑了,直接getPath就可以獲取到了。

好了,到此我們的所有疑問也就解決了。

小結(jié)

看完之后是不是發(fā)現(xiàn)思路很簡單但是實(shí)現(xiàn)起來很多的知識盲區(qū)呢?確實(shí)是這樣。但是當(dāng)我們把這些細(xì)節(jié)都解決了之后我們就會學(xué)到很多的東西,相當(dāng)于以點(diǎn)帶面。文中還有好多沒有詳解的:
ContentUris,BitmapFactory,Cursor,DocumentsContract等等。因?yàn)檫@是另外一塊比較大的內(nèi)容,如果要講的話將會涉及到很多內(nèi)容就很容易偏離我們的主題了,所以只要知道大概是什么就可以了。

參考資料

《第一行代碼》郭霖

以上就是Android 調(diào)用系統(tǒng)相冊選擇照片的詳細(xì)內(nèi)容,更多關(guān)于Android 調(diào)用系統(tǒng)相冊的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論