1. 程式人生 > >實訓日記第一天_zPopuwindow照相機

實訓日記第一天_zPopuwindow照相機

/**

  • 通過這個 activity 啟動的其他 Activity 返回的結果在這個方法進行處理 * 我們在這裡對拍照、相簿選擇圖片、裁剪圖片的返回結果進行處理 * @param requestCode 返回碼,用於確定是哪個 Activity 返回的資料 * @param resultCode 返回結果,一般如果操作成功返回的是 RESULT_OK * @param data 返回對應 activity 返回的資料 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK) { // 通過返回碼判斷是哪個應用返回的資料 switch (requestCode) { // / 拍照 case TAKE_PHOTO_REQUEST_CODE: cropPhoto(photoUri); break; // 相簿選擇 case CHOICE_FROM_ALBUM_REQUEST_CODE: cropPhoto(data.getData()); break; // 裁剪圖片 case CROP_PHOTO_REQUEST_CODE: File file = new File(photoOutputUri.getPath()); if (file.exists()) { Bitmap bitmap = BitmapFactory.decodeFile(photoOutputUri.getPath()); img.setImageBitmap(bitmap); // file.delete(); // 選取完後刪除照片 } else { Toast.makeText(this, “找不到照片”, Toast.LENGTH_SHORT).show(); } break; } } }

/**

  • 裁剪圖片 */ private void cropPhoto(Uri inputUri) { // 呼叫系統裁剪圖片的 Action Intent cropPhotoIntent = new Intent(“com.android.camera.action.CROP”);

    // 設定資料Uri 和型別 c cropPhotoIntent.setDataAndType(inputUri, “image/*”); // 授權應用讀取 Uri,這一步要有,不然裁剪程式會崩潰 cropPhotoIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); // 設定圖片的最終輸出目錄 cropPhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoOutputUri = Uri.parse(“file:////sdcard/image_output.jpg”)); startActivityForResult(cropPhotoIntent, CROP_PHOTO_REQUEST_CODE); }

/**

  • 從相簿選取 / private void choiceFromAlbum() { // 開啟系統圖庫的 Action,等同於: “android.intent.action.GET_CONTENT” Intent choiceFromAlbumIntent = new Intent(Intent.ACTION_GET_CONTENT); // 設定資料型別為圖片型別 choiceFromAlbumIntent.setType("image/"); startActivityForResult(choiceFromAlbumIntent, CHOICE_FROM_ALBUM_REQUEST_CODE); }

/** * 設定拍照得到的照片的儲存目錄,因為我們訪問應用的快取路徑並不需要讀寫記憶體卡的申請許可權, * 因此,這裡為了方便,將拍照得到的照片存在這個快取目錄中 / private void startCamera() { ] File file = new File(getExternalCacheDir(), “image.jpg”); try { if (file.exists()) { file.delete(); } file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } /* * 因 Android 7.0 開始,不能使用 file:// 型別的 Uri 訪問跨應用檔案,否則報異常, * 因此我們這裡需要使用內容提供器,FileProvider 是 ContentProvider 的一個子類, * 我們可以輕鬆的使用 FileProvider 來在不同程式之間分享資料(相對於 ContentProvider 來說) */ if (Build.VERSION.SDK_INT >= 24) { photoUri = FileProvider.getUriForFile(this, “com.jiyun.zuoye”, file); } else { photoUri = Uri.fromFile(file); // Android 7.0 以前使用原來的方法來獲取檔案的 Uri } } // 開啟系統相機的 Action,等同於:“android.media.action.IMAGE_CAPTURE” Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); // 設定拍照所得照片的輸出目錄 takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); startActivityForResult(takePhotoIntent, TAKE_PHOTO_REQUEST_CODE); }

//開啟照相機 private void CAERA() {

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(intent);
startCamera();

}

授權及相簿選取 方法 @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults);

switch (requestCode) {

    case 0:

        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            CAERA();
        } else {

            Toast.makeText(this, "授權失敗", Toast.LENGTH_SHORT).show();
        }

        break;
    // 開啟相簿選取:
    case 1:

        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

        } else {
            Toast.makeText(this, "讀寫記憶體卡內容許可權被拒絕", Toast.LENGTH_SHORT).show();
        }

        break;

}

}

popuwindow按鈕監聽_從相簿選取 choiceFromAlbum();

popuwindow按鈕監聽_拍照

if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA}, 0); } else {

CAERA();

}

popuwindow佈局

在這裡插入圖片描述

動態授予許可權 在這裡插入圖片描述

在這裡插入圖片描述

視窗popuwindow定義在這裡插入圖片描述

定義變數 在這裡插入圖片描述