gpt4 book ai didi

android - 在牛轧糖中从相机拍摄时图像未加载

转载 作者:行者123 更新时间:2023-11-30 00:01:40 27 4
gpt4 key购买 nike

我在从虚无中捕捉图像时调用此方法。

private void CallCameraFeature() {    
Intent cameraOpeningIntent = new
Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
String fileName = EmpConstants.startImgName +
new SimpleDateFormat(EmpConstants.PhotoFileFormat,
Locale.US).format(new Date());
File imgFile = new File(mContext.getFilesDir(), "images");
File outFile = new File(imgFile, fileName + ".jpg");
Uri photoURi = FileProvider.getUriForFile(mContext,
BuildConfig.APPLICATION_ID + ".provider", outFile);
cameraOpeningIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURi);
startActivityForResult(cameraOpeningIntent, REQUEST_IMAGE_CAPTURE);
}

}

我已经在

中创建了xml文件

values -> provider_paths.xml

将图片存放在该路径

provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths >
<files-path name="my_images" path="images/"/>
<files-path name="my_docs" path="docs/"/>
</paths>

这样定义的路径,用于存储图像 DCIM。

public String getEmpThumbImageDirPath() {
try {
return Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_
DCIM).toString() + EmpConstants.appDir;
}catch (Exception e) {
Log.d("eEmp/ImgDir", e.toString());
return "";
}

Image is not loading like this

相机正在打开并拍摄图像,但未加载图像。我犯了什么错误。

如有任何帮助,我们将不胜感激。

04-13 20:05:43.738 30272-30272/com.eftronics.android.eEmployee E/ContentValues: createImageFile: 目录创建成功。04-13 20:05:43.739 30272-30272/com.eftronics.android.eEmployee E/ContentValues:运行:图像文件夹路径为:/storage/emulated/0/FolderName/InsideFolderNameIFYOUWant04-13 20:05:43.739 30272-30272/com.eftronics.android.eEmployee E/ContentValues: createImageFile: 图像文件名为:imageName_152363014373904-13 20:05:49.791 30272-30272/com.eftronics.android.eEmployee E/ContentValues: createImageFile: 目录已经存在。04-13 20:05:49.792 30272-30272/com.eftronics.android.eEmployee E/ContentValues:运行:图像文件夹路径为:/storage/emulated/0/FolderName/InsideFolderNameIFYOUWant04-13 20:05:49.792 30272-30272/com.eftronics.android.eEmployee E/ContentValues: createImageFile: 图像文件名为:imageName_1523630149792

最佳答案

试试这个,它对我有用:

private void openCamera()
{
try
{
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null)
{
try
{
// Create the File where the photo should go
File photoFile = createImageFile();

// Continue only if the File was successfully created
if (photoFile != null)
{
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);

takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, CAPTURE_IMAGE_REQUEST_CODE);
}
else
{
Log.e(TAG, "openCamera: image was not captured.");
}
}
catch (Exception ex)
{
// Eor occurred while creating the File
ex.printStackTrace();
}
}
}
catch (Exception e)
{
Log.e(TAG, "openCamera: exception while opening camera:");
e.printStackTrace();
}
}

private String mCurrentPhotoPath;

private File createImageFile()
{
// Create an image file name
File sd = Environment.getExternalStorageDirectory();

File imageFolder = new File(sd.getAbsolutePath() + File.separator +
"FolderName" + File.separator + "InsideFolderNameIFYOUWant");

if (!imageFolder.exists())
{
if (imageFolder.mkdirs())
{
Log.e(TAG, "createImageFile: directory was created successfully.");
}
else
{
Log.e(TAG, "createImageFile: directory was not created.");
}
}
else
{
Log.e(TAG, "createImageFile: directory already exists.");
}

Log.e(TAG, "run: image folder path is: " + imageFolder.getAbsolutePath());

File image = null;
File mediaFile = new File(imageFolder + File.separator );
String imageFileName = "imageName_" + System.currentTimeMillis();
Log.e(TAG, "createImageFile: image file name is: " + imageFileName);

try
{
image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
mediaFile /* directory */);
}
catch (IOException e)
{
Log.e(TAG, "createImageFile: exception occurred while creating image file:\n");
e.printStackTrace();
}

if (image != null)
{
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
else
{
Log.e(TAG, "createImageFile: image was not created.");
return null;
}
}

将此添加到您的 list 文件中:

<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_paths"/>
</provider>

然后创建 xml 资源目录并添加 file_paths xml 文件并在其中添加:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="PathOfYourFolder" />
</paths>

在您的 Activity 结果方法中,只需使用 mCurrentPhotoPath 将图像加载到您的 ImageView 中。使用 Picasso 来做到这一点。

关于android - 在牛轧糖中从相机拍摄时图像未加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49818679/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com