欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android使用相機(jī)實(shí)現(xiàn)拍照存儲及展示功能詳解

 更新時間:2023年01月04日 15:25:54   作者:知奕奕  
這篇文章主要介紹了Android使用相機(jī)實(shí)現(xiàn)拍照存儲及展示功能,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧

無圖片處理款

配置存儲路徑映射

新建文件:res/xml/files.xml

使用 paths 標(biāo)簽映射一個存儲路徑,而 external-path 表示存儲路徑的根路徑位于外部存儲器內(nèi)

name 自定義名稱,可以隨便寫;

path 存儲的文件夾,寫一個小圓點(diǎn)表示直接使用根目錄文件夾

<?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 標(biāo)簽下添加一個 provider 標(biāo)簽

provider 標(biāo)簽的幾個主要屬性介紹:

  • android:name 固定的,使用 android 官方提供的 fileprovider
  • android:authorities 自己為該 provider 取的名字,建議格式:包名+fileprovider
  • android:exported 設(shè)置為 false 表示本應(yīng)用的其他 activity 不可以調(diào)用該 fileprovider 獲取的內(nèi)容!
  • android:grantUriPermissions 設(shè)置為 true 表示允許 uri 讀寫權(quán)限(這個必須開啟!)

meta-data 標(biāo)簽填入我們存儲文件的映射 xml,就是上一節(jié)我們編寫的文件

<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 官方自帶的圖標(biāo),挺實(shí)用的,大家可以多試試

<?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>

主代碼文件

由于內(nèi)容較為復(fù)雜,這里分層講,最后貼出完整代碼

首先全局定義三個延后初始化的變量

分別存儲我們的圖片 uri,以及兩個組件

特別注意! photoUri 的類型是 Uri 而不是 URI,這倆玩意完全不一樣,別看岔了!

class MainActivity : AppCompatActivity() {
    private lateinit var photoUri: Uri
    private lateinit var btn: ImageButton
    private lateinit var image: ImageView
    ...
}

之后就是按鈕點(diǎn)擊喚醒相機(jī)拍照,并存儲相片展示到 imageview 的過程

具體過程請看詳細(xì)注釋

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
        // 參數(shù)一:上下文
        // 參數(shù)二:我們之前在androidmanifest中注冊provider時自定義的authorities
        // 參數(shù)三:欲轉(zhuǎn)換成uri路徑的文件
        photoUri = FileProvider.getUriForFile(
            this@MainActivity,
            "com.zhiyiyi.login.fileprovider",
            file
        )
        // 使用隱式intent喚醒相機(jī)
        val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        // 傳入photouri,以保證我們的照片存儲到指定位置
        intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri)
        // 老樣子,不解釋
        startActivityForResult(intent, 1)
    }
}

onActivityResult 部分

內(nèi)容很簡單,即檢測響應(yīng)值并解碼圖片文件->顯示圖片

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 不負(fù)責(zé)權(quán)限申請,且根據(jù)作者真機(jī)調(diào)試,直接開啟相機(jī)并不需要請求任何權(quán)限,故再次做忽略處理

下一節(jié)講講述如何縮放以及調(diào)節(jié)圖片分辨率以達(dá)到最佳效果

到此這篇關(guān)于Android使用相機(jī)實(shí)現(xiàn)拍照存儲及展示功能詳解的文章就介紹到這了,更多相關(guān)Android使用相機(jī)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論