下面的代码用于尝试文件提供程序以准备android Nougat:
清单文件:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="/"/>
</paths>
file provider xml:
private Uri createImageFile() throws Exception {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), System.currentTimeMillis() + ".jpg");
//File file = new File(System.currentTimeMillis() + ".jpg");
Uri photoUri = null;
try{
photoUri = FileProvider.getUriForFile(this, "com.securefilesharing.fileprovider", file);
}
catch(Exception e){
Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
}
return photoUri;
}
View.OnClickListener ocl = new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
try {
uri = createImageFile();
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
cameraIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
} catch (Exception e) {
Log.e(TAG, TAG + ": " + e.toString());
}
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
};
构建URI并传递给相机意图:
angular.module('myApp', []).controller('myCtrl', ['$scope', function($scope){
$scope.myData = [
{
'car': 'Ford',
'color': [{'primary':'Black'},{'secondary':'White'}],
'number': '1, 2, 3',
'model': 'Figo'
}, {
'car': 'Ford',
'color': [{'primary':'Red'},{'secondary':'Black'}],
'number': '4,5',
'model': 'Endeavour'
},{
'car': 'Jaguar',
'color': [{'primary':'White'},{'secondary':'Red'}],
'number': '6',
'model': 'F-Type'
},
];
$scope.getData = function(obj){
$scope.color =[];
$scope.color.push(obj[0].primary);
$scope.color.push(obj[1].secondary);
console.log($scope.color);
};
}]);
我还做了activity.requestPermissions(forPermissions.toArray(new String [0]),PERMISSION_CODE);对于相机,读写外部文件
下面的是异常消息:
06-22 15:04:13.962:E / HomeSecureFileShareTestActivity(31851):HomeSecureFileShareTestActivity:java.lang.NullPointerException:尝试调用虚方法&#39; android.content.res.XmlResourceParser android.content.pm.ProviderInfo .loadXmlMetaData(android.content.pm.PackageManager,java.lang.String)&#39;在空对象引用上
答案 0 :(得分:5)
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.securefilesharing.fileprovider"
android:exported="false"
android:grantUriPermissions="true" >
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_path" />
</provider>
更改清单中的提供商部分... 参考:: https://developer.android.com/guide/topics/manifest/provider-element.html#enabled
答案 1 :(得分:0)
试试这个,
private Context mContext=YourActivity.this;
private static final int REQUEST = 112;
View.OnClickListener ocl = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (Build.VERSION.SDK_INT >= 23) {
String[] PERMISSIONS = {android.Manifest.permission.WRITE_EXTERNAL_STORAGE,android.Manifest.permission.READ_EXTERNAL_STORAGE,android.Manifest.permission.CAMERA};
if (!hasPermissions(mContext, PERMISSIONS)) {
ActivityCompat.requestPermissions((Activity) mContext, PERMISSIONS, REQUEST );
} else {
openCamera()
}
} else {
openCamera()'
}
}
};
获取权限结果
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openCamera();
} else {
Toast.makeText(mContext, "The app was not allowed to write in your storage", Toast.LENGTH_LONG).show();
}
}
}
}
检查marshmallow的权限
private static boolean hasPermissions(Context context, String... permissions) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
}
return true;
}
清单
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera"/>
<uses-feature android:name="android.hardware.camera.autofocus" />
openCamera Funtion
public void openCamera()
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
try {
uri = createImageFile();
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
} catch (Exception e) {
Log.e(TAG, TAG + ": " + e.toString());
}
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
createImageFile功能:
public Uri createImageFile() {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/TEST");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fName = "Image_" + n + ".jpg";
File file = new File(myDir, fName);
if (file.exists()) {
file.delete();
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Uri uriImage = Uri.fromFile(file);
return uriImage;
}
答案 2 :(得分:0)
File file = new File(Environment.getExternalStorageDirectory(), "/download/"+ GlobalVars.apkname+".apk");
System.out.println("file>>>>>"+file);
Uri fileUri = Uri.fromFile(file);
if (Build.VERSION.SDK_INT >= 24) {
fileUri = FileProvider.getUriForFile(MainActivity.this,getPackageName(), file);
System.out.println("File URI>>>>>"+fileUri);
//fileUri = FileProvider.get
}