網站首頁 編程語言 正文
在使用各類App的時候,尤其是在發布朋友圈、微博的時候,都會選擇配圖,進入手機相冊,選擇自己想要的照片,作為發布內容的一部分,這里就簡單介紹一下點擊圖片上傳的方法。
1、動態獲取權限
在Android 6.0之后,除了在清單文件中聲明權限之外,通常做的還有就是動態申請權限。
//從手機相冊中獲取圖片需要動態申請權限
if(ContextCompat.checkSelfPermission(this,
? ? ? ?Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
? ? ? ? ? ActivityCompat.requestPermissions(this,
? ? ? ? ? ? ? ? ? new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},ALBUM_CODE);
? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? //如果已經獲取權限,那么就直接拿
? ? ? ? ? ? ? ? ? ? takePhoto();
? ? ? ? ? ? ? ? }
//權限申請回調
? ? @Override
? ? public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
? ? ? ? switch (requestCode){
? ? ? ? ? ? case ALBUM_CODE:
? ? ? ? ? ? ? ? //獲取照片
? ? ? ? ? ? ? ? takePhoto();
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? }
2、跳轉到相冊,選擇照片
private void takePhoto() {
? ? ? ? Intent intent = new Intent(Intent.ACTION_PICK,null);
? ? ? ? intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
? ? ? ? ? ? ? ? "image/*");
? ? ? ? startActivityForResult(intent,ALBUM_CODE);
? ? }
涉及到的隱式跳轉的Action是ACTION_PICK,通過帶結果的回調startActivityForResult,將圖片信息帶回。
3、處理圖片數據,顯示在ImageView。
//從相冊返回的回調
? ? @Override
? ? protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
? ? ? ? super.onActivityResult(requestCode, resultCode, data);
? ? ? ? switch (requestCode) {
? ? ? ? ? ? case ALBUM_CODE:
? ? ? ? ? ? ? ? Uri uri = data.getData();
? ? ? ? ? ? ? ? if(uri != null){
// ? ? ? ? ? ? ? ? ? ?將圖片顯示在ImageView
? ? ? ? ? ? ? ? ? ? iv_goods.setVisibility(View.VISIBLE);
? ? ? ? ? ? ? ? ? ? //
? ? ? ? ? ? ? ? ? ? Glide.with(this).load(uri).into(iv_goods);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? break;
? ? ? ? }
將圖片顯示在ImagView中后,如果想要將圖片保存在數據庫,需要做一系列的轉換。
首先,SQLite數據庫支持的數據類型包括:int、String、text、Blob,其中Blob就是在存儲圖片等多媒體的時候,使用的類型。
其實Blob可以支持多種輸入類型,像圖片這類的資源,通常需要轉換為字節流,采用byte數組來存儲,因此在SQLite中,也可以使用byte數組來存儲圖片,因此需要將Drawable類型的圖片轉換為byte數組。
//Drawable對象轉換為byte數組
? ? public static byte[] getBytes(Drawable drawable){
? ? ? ? //將Drawable對象轉換為bitmap對象
? ? ? ? Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
? ? ? ? //創建輸出字節流
? ? ? ? ByteArrayOutputStream bos = new ByteArrayOutputStream();
? ? ? ? //壓縮
? ? ? ? bitmap.compress(Bitmap.CompressFormat.PNG, 1, bos);
? ? ? ? return bos.toByteArray();
? ? }
我這邊用的是Room組件
@ColumnInfo(name = "drawable")
? ? private byte[] drawable;
在獲取圖片資源的時候,獲取byte數組,將其轉換為Bitmap或者Drawable,就可以取出照片顯示。
?//byte數組轉換為Bitmap
? ? public static Bitmap getBitmap(byte[] bytes){
? ? ? ? //BitmapFactory
? ? ? ? Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
? ? ? ? return bitmap;
? ? }
原文鏈接:https://blog.csdn.net/qq_33235287/article/details/106128229
相關推薦
- 2023-07-14 react 利用antd-mobile實現樓層效果
- 2024-02-17 開發中SpringBoot項目jar包過大的解決辦法
- 2022-06-07 Python利用capstone實現反匯編_python
- 2023-10-25 對于Echarts實例化與銷毀的一些運用
- 2022-06-28 C#遞歸算法和排列算法_C#教程
- 2023-02-06 Golang泛型實現類型轉換的方法實例_Golang
- 2023-11-11 Python測網絡連通性、能否訪問某個網絡或者端口號<網絡檢測、ping主機、測試端口>
- 2022-09-04 C++實現ETW進行進程變動監控詳解_C 語言
- 最近更新
-
- 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同步修改后的遠程分支