網站首頁 編程語言 正文
無圖片處理款
配置存儲路徑映射
新建文件:res/xml/files.xml
使用 paths
標簽映射一個存儲路徑,而 external-path
表示存儲路徑的根路徑位于外部存儲器內
name 自定義名稱,可以隨便寫;
path 存儲的文件夾,寫一個小圓點表示直接使用根目錄文件夾
<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"> <external-path name="myphotos" path="." /> </paths>
配置 fileprovider
開啟 androidmanifest.xml 文件,在 application 標簽下添加一個 provider 標簽
provider 標簽的幾個主要屬性介紹:
- android:name 固定的,使用 android 官方提供的 fileprovider
- android:authorities 自己為該 provider 取的名字,建議格式:包名+fileprovider
- android:exported 設置為 false 表示本應用的其他 activity 不可以調用該 fileprovider 獲取的內容!
- android:grantUriPermissions 設置為 true 表示允許 uri 讀寫權限(這個必須開啟!)
meta-data 標簽填入我們存儲文件的映射 xml,就是上一節我們編寫的文件
<application ...> <activity android:name=".MainActivity" android:exported="true"> ... </activity> <provider android:name="androidx.core.content.FileProvider" android:authorities="com.zhiyiyi.login.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/files" /> </provider> </application>
布局文件
非常簡單,一個 imageview+imagebutton 即可
注意這里的 imagebutton 使用了 android 官方自帶的圖標,挺實用的,大家可以多試試
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:layout_margin="20dp" tools:context=".MainActivity"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal"> <ImageView android:id="@+id/image" android:layout_width="200dp" android:layout_height="200dp" /> <ImageButton android:id="@+id/imagebtn" android:layout_width="100dp" android:layout_height="60dp" android:src="@android:drawable/ic_menu_camera" /> </LinearLayout> </LinearLayout>
主代碼文件
由于內容較為復雜,這里分層講,最后貼出完整代碼
首先全局定義三個延后初始化的變量
分別存儲我們的圖片 uri,以及兩個組件
特別注意! photoUri 的類型是 Uri 而不是 URI,這倆玩意完全不一樣,別看岔了!
class MainActivity : AppCompatActivity() { private lateinit var photoUri: Uri private lateinit var btn: ImageButton private lateinit var image: ImageView ... }
之后就是按鈕點擊喚醒相機拍照,并存儲相片展示到 imageview 的過程
具體過程請看詳細注釋
override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // 初始化兩個組件 btn = findViewById(R.id.imagebtn) image = findViewById(R.id.image) btn.setOnClickListener { // 新建一個文件,他的位置位于外存根目錄下,且文件名為outputimage.jpg val file = File(getExternalFilesDir(null), "outputimage.jpg") // 為避免輸出流錯誤,需要捕獲錯誤 try { file.createNewFile() } catch (e: IOException) { e.printStackTrace() } // 使用FileProvider.getUriForFile獲取相片存儲的URI // 參數一:上下文 // 參數二:我們之前在androidmanifest中注冊provider時自定義的authorities // 參數三:欲轉換成uri路徑的文件 photoUri = FileProvider.getUriForFile( this@MainActivity, "com.zhiyiyi.login.fileprovider", file ) // 使用隱式intent喚醒相機 val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE) // 傳入photouri,以保證我們的照片存儲到指定位置 intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri) // 老樣子,不解釋 startActivityForResult(intent, 1) } }
onActivityResult 部分
內容很簡單,即檢測響應值并解碼圖片文件->顯示圖片
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) when (requestCode) { 1 -> { if (resultCode == RESULT_OK) { try { // 使用bitmap解碼文件 val bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(photoUri)) // 將圖片展示在imageview上面 image.setImageBitmap(bitmap) } catch (e: FileNotFoundException) { e.printStackTrace() } } } } }
完整代碼展示:
package com.zhiyiyi.login import android.content.Intent import android.graphics.BitmapFactory import android.net.Uri import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.os.IBinder import android.provider.MediaStore import android.util.Log import android.widget.ImageButton import android.widget.ImageView import androidx.core.content.FileProvider import com.zhiyiyi.login.databinding.ActivityMainBinding import java.io.File import java.io.FileNotFoundException import java.io.IOException import java.net.URI import kotlin.math.log class MainActivity : AppCompatActivity() { private lateinit var photoUri: Uri private lateinit var btn: ImageButton private lateinit var image: ImageView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) btn = findViewById(R.id.imagebtn) image = findViewById(R.id.image) btn.setOnClickListener { Log.d("fuck", "tnnd就是拍不了照") val file = File(getExternalFilesDir(null), "outputimage.jpg") Log.d("fuck", file.absolutePath) try { file.createNewFile() } catch (e: IOException) { e.printStackTrace() } photoUri = FileProvider.getUriForFile( this@MainActivity, "com.zhiyiyi.login.fileprovider", file ) val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE) intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri) startActivityForResult(intent, 1) } } override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { super.onActivityResult(requestCode, resultCode, data) when (requestCode) { 1 -> { if (resultCode == RESULT_OK) { try { val bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(photoUri)) image.setImageBitmap(bitmap) } catch (e: FileNotFoundException) { e.printStackTrace() } } } } } }
本 demo 不負責權限申請,且根據作者真機調試,直接開啟相機并不需要請求任何權限,故再次做忽略處理
下一節講講述如何縮放以及調節圖片分辨率以達到最佳效果
原文鏈接:https://blog.csdn.net/delete_you/article/details/127730740
相關推薦
- 2024-04-07 springmvc接受對象參數和MultipartFile參數的問題
- 2022-06-06 mui柵格布局的兩種方式(grid和list,宮格和列表)
- 2022-10-19 Pandas如何對Categorical類型字段數據統計實戰案例_python
- 2023-02-09 go?sync?Once實現原理示例解析_Golang
- 2022-03-14 怎么去掉Starting new HTTP connection 的提示
- 2022-01-09 webpack-code split代碼分割
- 2024-04-04 jQuery實現ajax語法,post請求發送數組對象(jquery)
- 2023-01-12 Python讀取mat(matlab數據文件)并實現畫圖_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支