在我的应用程序中,图像文件列表在回收视图中被放大。应用后对图像文件的元数据进行编辑。
我编写元数据的代码:-
DocumentFile fos = DocumentFile.fromFile(new File(fileIn));
ByteSource bytsInputStream = new ByteSourceFile(new File(fileIn));
byte[] byteArrayInputStream = bytsInputStream.getAll();
try {
ParcelFileDescriptor pfd = context.getContentResolver().
openFileDescriptor(fos.getUri(), "w");
FileOutputStream fileOutputStream =
new FileOutputStream(pfd.getFileDescriptor());
rewriter.updateExifMetadataLossy(byteArrayInputStream,fileOutputStream,outputSet);
fileOutputStream.close();
pfd.close();
}
catch (Exception e){e.printStackTrace();}我能够更新存储在手机内存中的图像文件,但是对于Sd卡图像,我会得到错误-->
java.io.FileNotFoundException: Permission denied我知道从android 5,我们需要获得许可使用SAF。我的许可代码:-
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.addFlags(
Intent.FLAG_GRANT_READ_URI_PERMISSION
| Intent.FLAG_GRANT_WRITE_URI_PERMISSION
| Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION);
startActivityForResult(intent, REQUEST_CODE_OPEN_DOCUMENT_TREE);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case REQUEST_CODE_OPEN_DOCUMENT_TREE:
if (resultCode == Activity.RESULT_OK) {
Uri treeUri = data.getData();
int takeFlags = data.getFlags();
takeFlags &= (Intent.FLAG_GRANT_READ_URI_PERMISSION |
Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
this.getContentResolver().takePersistableUriPermission(treeUri, takeFlags);
}
}
}现在我不知道如何处理这个treeUri。我希望我只在初始启动时获得SdCard许可一次。
,简单地说,我的问题与这个问题有关:-
发布于 2017-03-23 09:53:10
DocumentFile documentFile = DocumentFile.fromTreeUri(this, treeUri);在这里,treeUri是从SAF许可中得到的。意思是在OnActivityResult()中得到了什么。
DocumentFile fos =DocumentFile.fromFile(新文件(FileIn));
在这里,fileIn是您选择的文件。然后
String[] parts = (fileIn.getPath()).split("\\/");
for (int i = 3; i < parts.length; i++) {
if (documentFile != null) {
documentFile = documentFile.findFile(parts[i]);
}
}接下来,要获得byteArray,您需要使用以下代码
try {
InputStream iStream = getContentResolver().openInputStream(documentFile.getUri());
byte[] byteArrayInputStream = getBytes(iStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}在获得字节数组后,您希望继续您的代码,同时我在下面列出。
try {
ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(fos.getUri(), "w");
FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());
rewriter.updateExifMetadataLossy(byteArrayInputStream,fileOutputStream,outputSet);
fileOutputStream.close();
pfd.close();
}catch (Exception e){
e.printStackTrace();
}不,我想这会对你有帮助。如果你发现任何错误,请改正。谢谢
发布于 2016-08-19 12:19:26
将Uri保存在
takePersistableUriPermission()进入是绝对正确的。该权限一直保留到卸载或清除应用程序数据为止。下一步是保留Uri以供以后访问(即SharedPreferences)。
This会帮你的。
https://stackoverflow.com/questions/38998276
复制相似问题