我們首先來看下代碼:
作為一家“創(chuàng)意+整合+營銷”的成都網(wǎng)站建設(shè)機構(gòu),我們在業(yè)內(nèi)良好的客戶口碑。創(chuàng)新互聯(lián)提供從前期的網(wǎng)站品牌分析策劃、網(wǎng)站設(shè)計、成都做網(wǎng)站、成都網(wǎng)站設(shè)計、創(chuàng)意表現(xiàn)、網(wǎng)頁制作、系統(tǒng)開發(fā)以及后續(xù)網(wǎng)站營銷運營等一系列服務(wù),幫助企業(yè)打造創(chuàng)新的互聯(lián)網(wǎng)品牌經(jīng)營模式與有效的網(wǎng)絡(luò)營銷方法,創(chuàng)造更大的價值。//檢查權(quán)限 public void checkPermission() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); //發(fā)現(xiàn)沒有權(quán)限,調(diào)用requestPermissions方法向用戶申請權(quán)限,requestPermissions接收三個參數(shù),第一個是context,第二個是一個String數(shù)組,我們把要申請的權(quán)限 //名放在數(shù)組中即可,第三個是請求碼,只要是唯一值就行 } else { openAlbum();//有權(quán)限就打開相冊 } } public void openAlbum() { //通過intent打開相冊,使用startactivityForResult方法啟動actvity,會返回到onActivityResult方法,所以我們還得復(fù)寫onActivityResult方法 Intent intent = new Intent("android.intent.action.GET_CONTENT"); intent.setType("image/*"); startActivityForResult(intent, CHOOSE_PHOTO); } //彈出窗口向用戶申請權(quán)限 @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults);//彈出授權(quán)的窗口,這條語句也可以刪除,沒有影響 //獲得了用戶的授權(quán)結(jié)果,保存在grantResults中,判斷grantResult中的結(jié)果來決定接下來的操作 switch (requestCode) { case 1: if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { openAlbum(); } else { Toast.makeText(this, "授權(quán)失敗,無法操作", Toast.LENGTH_SHORT).show(); } break; default: break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (requestCode) { case CHOOSE_PHOTO: if (resultCode == RESULT_OK) { if (Build.VERSION.SDK_INT >= 19) { handleImageOnkitKat(data);//高于4.4版本使用此方法處理圖片 } else { handleImageBeforeKitKat(data);//低于4.4版本使用此方法處理圖片 } } break; default: break; } } @TargetApi(19) 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];//解析出數(shù)字格式的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())) { imagePath = getImagePath(uri, null); } displayImage(imagePath); } private void handleImageBeforeKitKat(Intent data) { Uri uri = data.getData(); String imagePath = getImagePath(uri, null); displayImage(imagePath); } //獲得圖片路徑 public String getImagePath(Uri uri, String selection) { String path = null; Cursor cursor = getContentResolver().query(uri, null, selection, null, null); //內(nèi)容提供器 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 bitImage = BitmapFactory.decodeFile(imagePath);//格式化圖片 mImage.setImageBitmap(bitImage);//為imageView設(shè)置圖片 } else { Toast.makeText(MainActivity.this, "獲取圖片失敗", Toast.LENGTH_SHORT).show(); } }