相机打开,允许我拍照,但不能将图像保存到目录中。我希望将文件保存到图片。我尝试放置getExternalStoragePublicDirectory
而不是getExternalFileDir
,但这会使应用返回初始的activity
。
我希望activity
拍照,然后允许我的应用从文件目录中手动选择该图片
//相机活动
package org.wit.shiploggerm1a2.activities
import android.Manifest.permission.CAMERA
import android.Manifest.permission.READ_EXTERNAL_STORAGE
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Bundle
import android.os.Environment
import android.provider.MediaStore
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.support.v4.content.FileProvider
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
import org.wit.shiploggerm1a2.R
import java.io.File
import java.io.IOException
import java.text.SimpleDateFormat
import java.util.*
class CameraActivity : AppCompatActivity() {
lateinit var imageView: ImageView
lateinit var captureButton: Button
val REQUEST_IMAGE_CAPTURE = 1
private val PERMISSION_REQUEST_CODE: Int = 101
private var mCurrentPhotoPath: String? = null;
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_camera)
imageView = findViewById(R.id.image_view)
captureButton = findViewById(R.id.btn_capture)
captureButton.setOnClickListener(View.OnClickListener {
if (checkPersmission()) takePicture() else requestPermission()
})
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when (requestCode) {
PERMISSION_REQUEST_CODE -> {
if ((grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED)
&& grantResults[1] == PackageManager.PERMISSION_GRANTED) {
takePicture()
} else {
Toast.makeText(this, "Permission Denied", Toast.LENGTH_SHORT).show()
}
return
}
else -> {
}
}
}
private fun takePicture() {
val intent: Intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
val file: File = createFile()
val uri: Uri = FileProvider.getUriForFile(
this,
"com.example.android.fileprovider",
file
)
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri)
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
val auxFile = File(mCurrentPhotoPath)
var bitmap: Bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath)
imageView.setImageBitmap(bitmap)
}
}
private fun checkPersmission(): Boolean {
return (ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA) ==
PackageManager.PERMISSION_GRANTED && ContextCompat.checkSelfPermission(this,
android.Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED)
}
private fun requestPermission() {
ActivityCompat.requestPermissions(this, arrayOf(READ_EXTERNAL_STORAGE, CAMERA), PERMISSION_REQUEST_CODE)
}
@Throws(IOException::class)
private fun createFile(): File {
val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
val storageDir: File = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
return File.createTempFile(
"JPEG_${timeStamp}_", /* prefix */
".jpg", /* suffix */
storageDir /* directory */
).apply {
mCurrentPhotoPath = absolutePath
}
}
}
//清单
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.wit.shiploggerm1a2">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:name=".main.MainApp"
android:allowBackup="true"
android:icon="@mipmap/shortcut_round"
android:label=""
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".activities.ShipLoggerActivity">
</activity>
<activity android:name=".activities.CameraActivity">
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_path">
</meta-data>
</provider>
<activity android:name=".activities.ShipLoggerListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>