我正在尝试从Android相机中拍摄照片,我想使用UUID使用自定义文件名将其保存(例如:f12b5700-1d92-11e9-ab14-d663bd873d93.jpg)。
在下面的代码中,在onActivityResult
中,我确实在photoPath中获得了 f12b5700-1d92-11e9-ab14-d663bd873d93.jpg ,但是当我检查设备上的实际图像时,保存为timestamp.jpg(例如:1548082653944.jpg)。我的问题是如何使图像以自定义名称保存?
private var photoURI: Uri? = null
private fun takePhoto()
{
val values = ContentValues()
values.put(MediaStore.Images.Media.TITLE, UUID.randomUUID().toString() + ".jpg")
photoURI = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(packageManager) != null)
{
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
startActivityForResult(intent, PHOTO_REQUEST_CODE);
}
}
在onActivityResult处理程序中,代码如下:
if (requestCode == PHOTO_REQUEST_CODE)
{
if (resultCode == Activity.RESULT_OK)
{
val proj = arrayOf(MediaStore.Images.Media.TITLE)
val cursor = contentResolver.query(photoURI!!, proj, null, null, null)
val index = cursor!!.getColumnIndex(MediaStore.Images.Media.TITLE)
cursor.moveToFirst()
photoPath = cursor.getString(index)
cursor.close()
Toast.makeText(this, photoPath, Toast.LENGTH_LONG).show()
}
else
{
capturedImage.setImageURI(null)
Toast.makeText(this, "Photo was not taken", Toast.LENGTH_SHORT).show()
}
}
答案 0 :(得分:0)
经过一些努力,我能够在CommonsWare链接的帮助下解决该问题: https://github.com/commonsguy/cw-omnibus/tree/v8.13/Camera/FileProvider
此外,我还从这些链接获得帮助:
https://guides.codepath.com/android/Accessing-the-Camera-and-Stored-Media
https://guides.codepath.com/android/Sharing-Content-with-Intents#sharing-files-with-api-24-or-higher
https://developer.android.com/training/camera/photobasics
诀窍是使用FileProvider。
我必须在AndroidManifest.xml中添加<provider>
工作代码如下:
if (Environment.getExternalStorageState() != Environment.MEDIA_MOUNTED)
{
Toast.makeText(this, "Storage is not available", Toast.LENGTH_LONG).show()
return
}
var imageName = UUID.randomUUID().toString() + ".jpg"
var output = File(Environment.getExternalStoragePublicDirectory(appName), imageName)
if (!output.parentFile.exists() && !output.parentFile.mkdir())
{
Toast.makeText(this, "Unable to create storage folder", Toast.LENGTH_LONG).show()
return
}
photoURI = FileProvider.getUriForFile(this, authority, output)
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(packageManager) != null)
{
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
startActivityForResult(intent, PHOTO_REQUEST_CODE);
}
提供者:
<provider
android:authorities="com.domain.appname.fileprovider"
android:name="android.support.v4.content.FileProvider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"/>
</provider>
file_paths.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="appname" path="appname" />
</paths>
相应地更改com.domain.appname和appname。