iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 17
1
自我挑戰組

練習程式系列 第 17

Android 拍照並顯示照片

  • 分享至 

  • xImage
  •  

教學來源:
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以上:
https://ithelp.ithome.com.tw/upload/images/20190926/20111994KmB3gfaqRD.png
按下show on disk,可以看到檔案所在的位置:
https://ithelp.ithome.com.tw/upload/images/20190926/20111994Hp3PdcFyEV.png

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().

參考:Android 的 ExternalStorage

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://ithelp.ithome.com.tw/upload/images/20190926/20111994NA3pIMGkMt.png
參考:
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檔案的方法:
https://ithelp.ithome.com.tw/upload/images/20190926/201119943X5JfUR5Me.png
檔案會在這裡:
https://ithelp.ithome.com.tw/upload/images/20190926/201119948B5kz93H5a.png

fileprovider 大概就是
把storage/emulated/0/temp/1591508490106.jpg
換成 content://com.example.test.fileprovider/my_fileproviders/temp/1591508490106.jpg


上一篇
java 物件導向練習 、 Graph BFS & DFS、Tree
下一篇
Android,grid view、camera
系列文
練習程式37
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言