教學來源:
Capture picture from camera - Android Studio Tutorial
Android中呼叫攝像頭拍照儲存,並在相簿中選擇圖片顯示
Android存储路径你了解多少?
一 要改AndroidManifest.xml:
android:name="androidx.core.content.FileProvider"
參考:
Unresolved class 'FileProvider'
二
getExternalStoragePublicDirectory 好像不能用?
所以先用
File file = File.createTempFile(String.valueOf(System.currentTimeMillis()), ".jpg", Environment.getExternalStorageDirectory());
整理:
官方教學:
https://developer.android.com/training/camera/photobasics#java
1 AVD就是模擬器,如果不連自己的手機當測試,就要載AVD。
這邊就是下載的手機模擬器,容量都3GB以上:
按下show on disk,可以看到檔案所在的位置:
2
這段代表如果這台android如果沒有相機功能,就不能再Google Play載這個app:
<manifest ... >
<uses-feature android:name="android.hardware.camera"
android:required="true" />
...
</manifest>
3
getExternalStoragePublicDirectory() -- >這台手機的所有app都可以使用這個相片。(不確定)
getExternalFilesDir()-- >只有這個app才可以使用這個相片(不確定)
這個想法應該是錯的,官網:
getExternalStorageDirectory:
Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.
In devices with multiple shared/external storage directories, this directory represents the primary storage that the user will interact with. Access to secondary storage is available through getExternalFilesDirs(String), getExternalCacheDirs(), and getExternalMediaDirs().
Environment.getExternalStorageDirectory() deprecated in API level 29 java
4
目前模擬器找不到圖片檔案位置。不過這個檔案:
C:\Users\使用者\AppData\Local\Android\sdk\system-images\android-25\google_apis\x86\system.img
是img光碟映像檔,難道裡面放圖片嗎?(不確定)
參考:
https://stackoverflow.com/questions/13095718/where-are-android-emulator-image-stored#
5
Java.io.File.createTempFile()方法實例
File.createTempFile用這個方法創建的檔案會在C:\Users\使用者\AppData\Local\Temp
6
FileProvider - IllegalArgumentException: Failed to find configured root
還不太懂fileprovider是什麼,不過要注意file_paths.xml這個檔案的寫法,
創建xml檔案的方法:
檔案會在這裡:
fileprovider 大概就是
把storage/emulated/0/temp/1591508490106.jpg
換成 content://com.example.test.fileprovider/my_fileproviders/temp/1591508490106.jpg