Android調用系統相機並解決兩大問題


前言

源碼Demo:請點擊此處
Android調用系統相機會遇到的兩大問題:

  • 1.指定存儲圖片路徑,Android7.0及之后的機型調用系統相機會拋出android.os.FileUriExposedException異常
  • 2.指定存儲圖片路徑,調用系統相機返回 intent 為:null

問題《一》

  • Android 7.0后系統禁止應用向外部公開file://URI ,因此需要FileProvider來向外界傳遞URI。所以針對安卓7.0及其之后的系統需要做一個適配。
  • 實際開發中,推薦該方式。知道文件路徑,可以根據需求執行相應壓縮處理。

開始代碼示例(Android Studio, SdkVersion 29)

  • 1️⃣AndroidManifest.xml 清單文件中添加所需權限
<!--相機權限-->
<uses-permission android:name="android.permission.CAMERA" />
<!--SD卡權限-->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
  • 2️⃣ activity_play_photo(PlayPhotoActivity的xml界面)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ImageView
        android:id="@+id/ivMyPhoto"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
    <Button
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:layout_alignParentBottom="true"
        android:gravity="center"
        android:onClick="playPhoto"
        android:padding="16dp"
        android:text="拍照(原圖-路徑獲取)"
        android:textColor="#FF212121"
        android:textSize="16sp"
        android:textStyle="bold" />
</RelativeLayout>
  • 3️⃣ PlayPhotoActivity(activity中調用相機拍照並返回展示圖片)
public class PlayPhotoActivity extends BaseActivity {
    //定義一個文件夾路徑
    private String localPath = MyApplication.localPath + File.separator + "123";
    private ImageView ivMyPhoto;
    private File photoFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_photo);
        ivMyPhoto = findViewById(R.id.ivMyPhoto);

        photoFile = new File(localPath, "temp.png");
        if ((photoFile.getParentFile() != null) && (!photoFile.getParentFile().exists())) {
            photoFile.getParentFile().mkdirs();
        }
        Log.e("相機", "路徑-localPath:" + localPath);
    }

    //相機點擊事件:打開照相機(該方式獲取到的圖片是原圖)
    public void playPhoto(View view) {
        //創建打開本地相機的意圖對象
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //設置圖片的保存位置(兼容Android7.0)
        Uri fileUri = getUriForFile(this, photoFile);
        //指定圖片保存位置
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
        //開啟意圖
        startActivityForResult(intent, 100);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        //拍照完成后返回調用
        if (resultCode == RESULT_OK) {
            if (requestCode == 100) {
                //該方式獲取到的圖片是原圖
                FileInputStream fis = null;
                try {
                    fis = new FileInputStream(photoFile);
                    Bitmap bitmap = BitmapFactory.decodeStream(fis);
                    ivMyPhoto.setImageBitmap(bitmap);
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        if (fis != null)
                            fis.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } 
        }
    }

    private Uri getUriForFile(Context context, File file) {
        Uri fileUri;
        if (Build.VERSION.SDK_INT >= 24) {
            //參數:authority 需要和清單文件中配置的保持完全一致:${applicationId}.xxx
            fileUri = FileProvider.getUriForFile(context, context.getPackageName() + ".xxx", file);
        } else {
            fileUri = Uri.fromFile(file);
        }
        return fileUri;
    }
}
  • 4️⃣ 清單文件配置
  1. SdkVersion 29之前使用:android.support.v4(下述)
    android:name="android.support.v4.content.FileProvider"
  2. SdkVersion 29開始使用:androidx(下述)
    android:name="androidx.core.content.FileProvider"
  3. authorities可以隨意定義(默認規程:采用本應用包名+定義串)
    android:authorities="包名.xxx"
        <provider
            android:name="androidx.core.content.FileProvider"
            android:authorities="${applicationId}.xxx"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
  • 5️⃣在 res 目錄下創建 xml 目錄,並在res/xml目錄下創建文件:file_paths(代碼如示)
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <root-path
        name="root"
        path="" />
    <!--files-path  相當於 getFilesDir()-->
    <files-path
        name="files"
        path="path" />
    <!--cache-path  相當於 getCacheDir()-->
    <cache-path
        name="cache"
        path="path" />
    <!--external-path  相當於 Environment.getExternalStorageDirectory()-->
    <external-path
        name="external"
        path="path" />
    <!--external-files-path  相當於 getExternalFilesDir("") -->
    <external-files-path
        name="external-files"
        path="path" />
    <!--external-cache-path  相當於 getExternalCacheDir() -->
    <external-cache-path
        name="external-cache"
        path="path" />
</paths>

問題《二》

  • 在調用系統相機的時候,如果傳入了:指定的路徑(文件保存地址),那么在activity的回調方法:onActivityResult 中,intent對象會是null。
  • 如問題一的示例代碼:onActivityResult的intent對象亦是null
  • 如何解決呢?可以參考下述代碼(但實際開發中,不推薦該方式,該方式獲取到的圖片數據是Android系統壓縮后的圖片。)

開始代碼示例(Android Studio, SdkVersion 29)

  • 1️⃣ 參考《問題一》第一步
  • 2️⃣ 參考《問題一》第二步
  • 3️⃣ PlayPhotoActivity(activity中調用相機拍照並返回展示圖片)
public class PlayPhotoActivity extends BaseActivity {
    private ImageView ivMyPhoto;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_play_photo);
        ivMyPhoto = findViewById(R.id.ivMyPhoto);
    }
    //相機點擊事件:打開照相機(該方式獲取到的圖片是縮略圖)
    public void playPhoto(View view) {
        //創建打開本地相機的意圖對象
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //如果intent指定了存儲圖片的路徑,那么onActivityResult回調中Intent對象就會為null
        //intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        //開啟意圖
        startActivityForResult(intent, 200);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);
        //拍照完成后返回調用
        if (resultCode == RESULT_OK) {
            if (requestCode == 200) {
                //該方式獲取到的圖片是縮略圖
                Bundle bundle = intent.getExtras();
                Bitmap bitmap = (Bitmap) bundle.get("data");
                ivMyPhoto.setImageBitmap(bitmap);
            }
        }
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM