gpt4 book ai didi

android - FileProvider 非常困惑

转载 作者:行者123 更新时间:2023-12-01 11:16:55 25 4
gpt4 key购买 nike

我有一个应用程序,我在其中捕获图像并将其设置为 ImageView,然后将其上传到服务器。但是,每当我捕获图像时,图像都不会显示,当我尝试上传图像时,我会得到 FileNotFoundException,因为路径不包含图像。

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.android.imageuploader.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="Image Uploader" path="Android/data/com.example.android.imageuploader/files/Pictures" />
</paths>

当我创建图像文件时,我使用它,
private File createImageFile() {
String timeStamp = new SimpleDateFormat(
getString(R.string.time_stamp_format), Locale.getDefault())
.format(new Date());
String fileName = getString(R.string.file_name_format, timeStamp);
File storageDirectory =
new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), getString(R.string.app_name));
if (!(storageDirectory.exists() || storageDirectory.mkdirs())) {
Helper.showToastMessage(mContext, getString(R.string.warn_storage_dir));
}
return new File(storageDirectory.getPath() + File.separator + fileName);
}

实际上,我将图像保存在内部存储目录“图片”中,并在其中创建应用程序名称后的文件夹,其中所有图像都保存在其中。但是在将图像设置为 ImageView 时,我得到了不同的文件路径说,
Image Uploader/Pictures/Image Uploader/20180406_101234.jpg

这就是为什么图像没有显示和上传的原因。

我哪里出错了,我无法弄清楚。请帮忙。

最佳答案

这就是我正在做的事情,这很完美。
AndroidManifest.xml

   <provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.yourpackagename.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_path"/>
</provider>
provider_path.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="/storage/emulated/0" path="."/>
</paths>
createImageFile()确保您具有读写外部存储权限
private File createImageFile() throws IOException
{

String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "MYAPPNAME-" + timeStamp + ".png";
File mediaStorageDir = new File(Environment.getExternalStorageDirectory(),
"YourAppFolder");
File storageDir = new File(mediaStorageDir + "/Profile_Images");
if (!storageDir.exists())
{
storageDir.mkdirs();
}
File image = new File(storageDir, imageFileName);
return image;
}
click listener on button to take camera image
===Global Variables===
Uri mUri;
private static final int CAMERA_IMAGE_RESULT = 202;
private static final String CAPTURE_IMAGE_FILE_PROVIDER = "com.yourpackagename.fileprovider";
===Global Variables===

takeImageBTN.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
File file = null;
try
{
file = createImageFile();
mUri = FileProvider.getUriForFile(this,
CAPTURE_IMAGE_FILE_PROVIDER, file);

Log.d("uri", mUri.toString());
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);
startActivityForResult(cameraIntent, CAMERA_IMAGE_RESULT);
}
catch (IOException e)
{
e.printStackTrace();
}

}
});

最后 onActivityResult()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);

switch (requestCode)
{
case CAMERA_IMAGE_RESULT:
{
if (resultCode == RESULT_OK)
{
if (mUri != null)
{
Log.d("uriPath", mUri.getPath().replace("//", "/"));
String profileImageFilepath = mUri.getPath().replace("//", "/");
Log.d("path", profileImageFilepath);
profileIV.setImageURI(mUri);
/*Your Asynctask to upload Image using profileImageFilepath*/
new PostDataAsyncTask().execute(profileImageFilepath);

}
}
break;
}
}
}

获取运行时许可
  <!-- == External Storage == -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

关于android - FileProvider 非常困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49685461/

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