无法打开/sys/bus/msm_subsys/devices/subsys0/name:权限被拒绝



这是一个Xamarin应用程序。我编辑了一张照片并试图保存它。对于一些图像,它保存得很好。其他图像屏幕变黑,调试器退出。

我在日志中发现了这个:

12-13 19:35:42.682 E/JavaBinder(31213): !!! FAILED BINDER TRANSACTION !!!  (parcel size = 1112628)
Thread finished: <Thread Pool> #2
The thread 0x2 has exited with code 0 (0x0).
Thread finished: <Thread Pool> #3
The thread 0x3 has exited with code 0 (0x0).
Thread started: <Thread Pool> #8
[0:] Java.Lang.RuntimeException: Failure from system ---> Android.OS.TransactionTooLargeException: data parcel size 1112628 bytes
--- End of inner exception stack trace ---

也许有些文件刚好超过了某个文件大小限制。这是相关代码:

var pickerIntent = new Intent (this._context, typeof (SavePickerActivity));
pickerIntent.SetFlags (ActivityFlags.NewTask);
pickerIntent.PutExtra("fileName", fileName);
pickerIntent.PutExtra("fileBytes", fileBytes    
this._context.StartActivity (pickerIntent); // <-- CRASH!

也许我应该给它一个流而不是字节数组?

我不是说这是答案,但我不确定是否有答案。

无论如何:

1.将文件写入缓存目录

private async Task<string> WriteFileToCacheDirectory(string fileName, byte[] fileBytes)
{
File outputDir = this._context.CacheDir;
File file = new File(outputDir, fileName);
FileOutputStream fos = new FileOutputStream(file.AbsolutePath);
await fos.WriteAsync(fileBytes);
fos.Flush();
fos.Close();
return file.AbsolutePath;
}

2.执行意图

var pathToFile = await WriteFileToCacheDirectory(fileName, fileBytes);
var pickerIntent = new Intent(this._context, typeof(SavePickerActivity));
pickerIntent.SetFlags(ActivityFlags.NewTask);
pickerIntent.PutExtra("fileName", fileName);
pickerIntent.PutExtra("path", pathToFile);
this._context.StartActivity(pickerIntent);
...

3.活动开始(在我的情况下是我的SavePickerActivity.cs(

[Activity (ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize)]
[Preserve (AllMembers = true)]
public class SavePickerActivity : Activity
{
string fileName;
string filePath;
protected override void OnCreate (Bundle savedInstanceState)
{
base.OnCreate (savedInstanceState);
var fileNameWithExtension = this.Intent.GetStringExtra("fileName");
var fileExtension = Path.GetExtension(fileNameWithExtension);
var mimeType = Android.Webkit.MimeTypeMap.Singleton.GetMimeTypeFromExtension(fileExtension.Substring(1));
fileName = fileNameWithExtension.Replace(fileExtension, "");
filePath = Intent.GetStringExtra("path");
var intent = new Intent(Intent.ActionCreateDocument);
intent.AddCategory(Intent.CategoryOpenable);
intent.SetType(mimeType);
intent.PutExtra(Intent.ExtraTitle, fileNameWithExtension);
// https://stackoverflow.com/questions/9080109/android-image-picker-for-local-files-only
// https://stackoverflow.com/questions/50980397/when-using-intent-actionopendocumenttree-can-you-allow-the-user-to-save-in-a-loc/51011346?noredirect=1#comment89016456_51011346
// https://stackoverflow.com/questions/51012702/whats-the-correct-way-to-save-a-file-when-using-the-content-sheme/51013020?noredirect=1#comment89020123_51013020 // note that I said it worked here. i said it worked prematurely because i never got it to work completely (even after wasting too much time trying to write a MCW). 
// when saving remotely it saves the file with 0 bytes. so only let it save locally.
intent.PutExtra(Intent.ExtraLocalOnly, true);
StartActivityForResult(Intent.CreateChooser(intent, "Select Save Location"), 43);
}
...

4.OnActivityResult从缓存目录中读取文件并保存

protected async override void OnActivityResult (int requestCode, [GeneratedEnum] Result resultCode, Intent data)
{
base.OnActivityResult (requestCode, resultCode, data);
if (resultCode == Result.Canceled && requestCode == 43) {
// Notify user file picking was cancelled.
OnDirectoryPickCancelled ();
Finish ();
} else {
System.Diagnostics.Debug.Write (data.Data);
try {
// read the file out of the cache dir
var file = new Java.IO.File(filePath);
var fileLength = (int)file.Length();
byte[] fileBytes = new byte[fileLength];
using (var inputStream = new FileInputStream(filePath))
{
await inputStream.ReadAsync(fileBytes, 0, fileLength);
}
// write the file to the path chosen by the user
using (var pFD = ContentResolver.OpenFileDescriptor(data.Data, "w"))
using (var outputSteam = new FileOutputStream(pFD.FileDescriptor))
{
outputSteam.Write(fileBytes);
}
...  

相关内容

最新更新