寻找一些关于错误的帮助我正在尝试存储用相机拍摄的照片我试图开发的应用程序。错误是
java.lang.IllegalArgumentException:无法找到包含/ storage / emulated / 0 / Pictures / JPEG20161108_153704 _
的已配置根目录logcat在我的代码中指向调用FileProvider.getUriForFile的行中的此方法。
private void dispatchTakePhoto() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(takePictureIntent); // this worked originally
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, ""+e);
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(TallyActivity2.this,
"com.example.bigdaddy.pipelinepipetally.fileprovider", photoFile);
takePictureIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
此方法用于创建图像文件
private File createImageFile() throws IOException {
/* Create an image file name */
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
String imageFileName = "JPEG" + timeStamp + "_";
File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsoluteFile(), imageFileName);
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
/* Tried this also, not working. Leaving for debugging.
File image = File.createTempFile(
imageFileName,
".jpg",
storageDir
);*/
File image = new File(path, imageFileName);
try {
/* Making sure the Pictures directory exist.*/
path.mkdir();
storageDir.createNewFile();
}catch (Exception e) {
e.printStackTrace();
}
/* Save a file: path for use with ACTION_VIEW intents */
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
这是onActivityResult()
方法,我希望使用saveImage()方法将捕获的图像保存到类中,并将缩略图设置为ImageView
。 saveImage()方法返回一个字节,因此我可以通过Intent将Bundle中的字节传递给另一个全屏活动,如果用户点击缩略图ImageView
,它将显示在该活动中。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
/* If it's equal to my REQUEST_IMAGE_CAPTURE var, we are all good. */
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
/*Saving to the Pipe class with the saveImage() method below.*/
sDummyImagePicByte = saveImage(imageBitmap);
/* Going ahead an setting the thumbnail here for the picture taken*/
mPipePicImage.setImageBitmap(imageBitmap);
Log.i(TAG,Arrays.toString(sDummyImagePicByte)+" after assignment from saveImage()");
}
}
以下是Manifest.xml
文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.bigdaddy.pipelinepipetally">
<uses-permission android:name="android.permission.READ_CONTACTS"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"/>
<application
android:allowBackup="true"
android:debuggable="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme"
tools:ignore="HardcodedDebugMode">
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustResize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".TallyActivity2"
android:windowSoftInputMode="adjustResize">
</activity>
<activity
android:name=".JobAndDbActivity"
android:windowSoftInputMode="adjustResize">
</activity>
<activity
android:name=".ExistingTallyActivity"
android:windowSoftInputMode="adjustResize">
</activity>
<activity android:name=".ImageToFullscreen"
android:windowSoftInputMode="adjustResize">
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.bigdaddy.pipelinepipetally.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths">
</meta-data>
</provider>
</application>
</manifest>
这是我创建的file_paths.xml
文件并放在app / res / xml /文件夹中(我也创建了它)。不确定这是否是正确的位置(对于文件夹)。
<paths >
<files-path name="my_images" path="files/"/>
...
</paths>
这也是onRequestPermissionsResult()
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[],
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_CAMERA:
/* if request is canceled, the result arrays are empty */
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
/* Permissions granted so the mPermissionIsGranted boolean is set to true*/
mPermissionIsGranted = true;
} else {
/*
Permissions denied so the mPermissionIsGranted boolean stays false here and
providing a Toast message to the user, letting them know that camera permissions
are required for this feature.
*/
Toast.makeText(getApplicationContext(),"Camera permissions required\nfor this" +
"feature.",
Toast.LENGTH_LONG).show();
/* Continuing to hold the false setting to this boolean since not granted.*/
mPermissionIsGranted = false;
}
break;
/* For accessing and writing to the SD card*/
case MY_PERMISSIONS_REQUEST_SD_CARD:
/* if request is canceled, the result arrays are empty */
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
/* Permissions granted so the mPermissionIsGranted boolean is set to true*/
mPermissionIsGranted = true;
} else {
/*
Permissions denied so the mPermissionIsGranted boolean stays false here and
providing a Toast message to the user, letting them know that camera permissions
are required for this feature.
*/
Toast.makeText(getApplicationContext(),"SD Card permissions required\nfor this"+
"feature.",
Toast.LENGTH_LONG).show();
/* Continuing to hold the false setting to this boolean since not granted.*/
mPermissionIsGranted = false;
}
break;
/* For the GPS location permissions.*/
default: MY_PERMISSIONS_REQUEST_FINE_LOCATION:
/* Still to be implemented .*/
break;
}
}
我非常感谢你对此有任何帮助。我还是新手,学习Android。提前谢谢。
答案 0 :(得分:12)
它的晚了......但我终于能够解决了。
根据documentation:FilterList filterList = new FilterList(/*FilterList.Operator.MUST_PASS_ONE*/);
String[] matches=new String[]{"1609","1610"};
for (String match:matches) {
byte[] rk = Bytes.toBytesBinary("??" + match + "??????????????");
byte[] fuzzyVal = new byte[]{1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
List<Pair<byte[], byte[]>> pairs = new ArrayList<>();
pairs.add(new Pair<>(rk, fuzzyVal));
filterList.addFilter(new FuzzyRowFilter(pairs));
}
Scan scan_fuzzy=new Scan();
scan_fuzzy.setFilter(filterList);
ResultScanner rs_fuzzy = table.getScanner(scan);
for( Result result : rs_fuzzy) {
System.out.println(result);
System.out.println("Value: " + Bytes.toString(result.getValue(FAMILY,COLUMN)));
}
所以使用
The path component only corresponds to the path that is returned by getExternalFilesDir() when called with Environment.DIRECTORY_PICTURES.
而不是
getExternalFilesDir(Environment.DIRECTORY_PICTURES)
你的道路应该是这样的:
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
如果你在Picture目录下有一个子目录,那就写下来。
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="InstructiveRide" path="Android/data/com.package.ride/files/Pictures"/>
</paths>
答案 1 :(得分:5)
<files-path name="my_images" path="files/"/>
这指向files/
内的getFilesDir()
目录。但是,这不是createImageFile()
放置文件的位置。相反,它正在使用Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
。你需要:
确定哪个位置正确(或选择其中任何一个选项),然后
同步实施
答案 2 :(得分:5)
我曾经为File provider的所有路径提供样板代码。您将永远不会遇到此类错误。
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external" path="." />
<external-files-path name="external_files" path="." />
<cache-path name="cache" path="." />
<external-cache-path name="external_cache" path="." />
<files-path name="files" path="." />
</paths>
有关更多详细信息,您可以选中FileProvider - Specifying available Files
答案 3 :(得分:4)
我也在尝试升级我的应用程序以避免FileUriExposedException。我想达到实际问题中提到的相同。我设法与你合作 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
只有技巧才能使用以下路径
scala> val r = List(1, 2, 3).foldLeft(0) _ + _
r: String => String = <function1>
scala> r("a")
res5: String = <function1>a
scala> r(" bbbb")
res6: String = <function1> bbbb
我从以下博客中得到了这个提示。
https://proandroiddev.com/sharing-files-though-intents-are-you-ready-for-nougat-70f7e9294a0b
我花了几个小时才弄明白。我希望有时候安全。
答案 4 :(得分:2)
我的问题是我在build.gradle
文件中为debug
模式定义了我的应用程序ID的后缀,当我在调试模式下运行应用程序时,包名称与什么不兼容我在代码中提到过。最好在xml
和java
代码中设置applicationId(packagename)变量。
为您的提供者集合执行此操作:
android:authorities="${applicationId}.fileprovider"
并在你的java代码中:
Uri photoURI = FileProvider.getUriForFile(context,
getApplicationContext().getPackageName()+".fileprovider",
并且对于您的提供者路径,为不同的构建变体(发布和调试)创建两个不同的文件
发布:
<external-path name="my_images" path="Android/data/com.android.example/files/Pictures" />
和调试:
<external-path name="my_images" path="Android/data/com.android.example.debug/files/Pictures" />