我在使用适用于Chrome的App Runtime编译的适用于Chrome操作系统的Android应用中写入外部文件时遇到问题。这个应用程序功能可以在两个Android测试设备上运行(一个API 19,与ARC相同,我相信),但在Chromebook上测试时总是失败。我的应用的minSdkVersion
为19
。
我的应用是文本编辑器,我想要实现的是使用Intent.ACTION_OPEN_DOCUMENT
加载文本,在此过程中保存文件Uri
。稍后,当我想要保存时,我可以使用此Uri
来访问该文件并更新其内容,而无需在每次要保存文件时使用“共享”对话框来打扰用户(或者如果他们设置了自动保存首选项。如果有ACTION_SEND
,我的应用默认为FileNotFoundException
意图。
这是相关代码。任何人都可以提出一个解决方法,或者我可以做出哪些改进?
public class EditorHost extends AppCompatActivity implements EditorFragment.EditorInterface {
private static final int REQUEST_OPEN = 15;
//...
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mEditorFragmentChild == null)
mEditorFragmentChild = (EditorFragment) mFragmentManager.findFragmentByTag(EDITOR_FRAGMENT_TAG);
switch (item.getItemId()) {
case R.id.load_file:
open();
break;
case R.id.save_file:
if (mEditorFragmentChild.getUri() == null){
shareTask();
break;
}
mEditorFragmentChild.saveTask();
break;
case R.id.share:
mIntent = new Intent(Intent.ACTION_SEND);
mIntent.putExtra(Intent.EXTRA_TEXT, mEditorFragmentChild.mExtendedEditText.getText().toString());
mIntent.setType("text/plain");
startActivity(Intent.createChooser(mIntent, getString(R.string.share_title)));
break;
@TargetApi(Build.VERSION_CODES.KITKAT)
private void open(){
mIntent = new Intent().setType("text/plain");
startActivityForResult(mIntent.setAction(Intent.ACTION_OPEN_DOCUMENT).addCategory(Intent.CATEGORY_OPENABLE), REQUEST_OPEN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData){
if (resultCode == Activity.RESULT_OK){
Uri uri = null;
if (resultData != null) {
uri = resultData.getData();
if (requestCode == REQUEST_OPEN) {
Cursor c = getContentResolver().query(uri,null, null, null, null);
if (c != null && c.moveToFirst()){
title = c.getString(c.getColumnIndex(OpenableColumns.DISPLAY_NAME));
String[] cols = c.getColumnNames();
for (int i = 0; i < c.getColumnCount(); i++){
Log.e(LOG_TAG, cols[i]);
}
content = readFromFile(uri);
if (title == null || title.equals("")){
title = getString(R.string.app_name);
}
getSupportActionBar().setTitle(title);
mEditorFragmentChild.updateTextFromFile(content.trim());
mEditorFragmentChild.setUri(uri.toString());
if (!c.isClosed()) {
c.close();
}
}
}
}
}
}
并在EditorFragment
班级中:
public class EditorFragment extends Fragment implements ... {
//...
String getUri(){
return mUri;
}
void setUri(String newUri){
mUri = newUri;
}
void saveTask(){
if (mUri == null || fromShareTask) {
fromShareTask = false;
return;
}
try {
OutputStream outputStream = mAppCompatActivity.getContentResolver().openOutputStream(Uri.parse(mUri));
outputStream.write(mExtendedEditText.getText().toString().getBytes());
outputStream.close();
//Toast.makeText(mAppCompatActivity, mAppCompatActivity.getString(R.string.file_updated_confirmation), Toast.LENGTH_SHORT).show();
} catch (IOException e){
e.printStackTrace();
dispatchShareEvent();
Toast.makeText(mAppCompatActivity, mAppCompatActivity.getString(R.string.save_failed), Toast.LENGTH_LONG).show();
mExtendedEditText.setText(e.toString() + "\n\n" + exceptionStacktraceToString(e) + "\n\n" + "mUri is " + mUri);
}
}
最后,错误消息(从上面重复一遍,这只发生在我的Chromebook而不是Android测试设备上):
java.io.FileNotFoundException: Can't access data/data/org.chromium.arc/external/(etc.)...
谢谢!
修改
虽然我无法弄清楚如何从我的Chromebook获取堆栈跟踪,但我可以添加following method from the accepted answer on this question将其打印到EditText
。我还在最后打印了Uri
。这是输出:
java.io.FileNotFoundException: Can't access /data/data/org.chromium.arc/external/E5783234425D1719508FC512B2BEE2A5/1kTest.txt
java.io.FileNotFoundException: Can't access /data/data/org.chromium.arc/external/E5783234425D1719508FC512B2BEE2A5/1kTest.txt
at com.android.providers.media.MediaProvider.checkAccess(MediaProvider.java:4620)
at com.android.providers.media.MediaProvider.openFileAndEnforcePathPermissionsHelper(MediaProvider.java:4569)
at com.android.providers.media.MediaProvider.openFile(MediaProvider.java:4493)
at android.content.ContentProvider.openAssetFile(ContentProvider.java:1211)
at android.content.ContentProvider.openAssetFile(ContentProvider.java:1274)
at android.content.ContentProvider$Transport.openAssetFile(ContentProvider.java:314)
at android.content.ContentProvider$Transport.openAssetFile(Native Method)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:926)
at android.content.ContentResolver.openOutputStream(ContentResolver.java:673)
at android.content.ContentResolver.openOutputStream(ContentResolver.java:649)
at com.werdpressed.partisan.filterforchrome.EditorFragment.saveTask(EditorFragment.java:370)
at com.werdpressed.partisan.filterforchrome.EditorHost.onOptionsItemSelected(EditorHost.java:119)
at android.app.Activity.onMenuItemSelected(Activity.java:2619)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:353)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:144)
at android.support.v7.internal.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:99)
at android.support.v7.app.AppCompatDelegateImplV7.onMenuItemSelected(AppCompatDelegateImplV7.java:538)
at android.support.v7.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:802)
at android.support.v7.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:153)
at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:949)
at android.support.v7.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:939)
at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:598)
at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:139)
at android.view.View.performClick(View.java:4440)
at android.view.View$PerformClick.run(View.java:18407)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:5181)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.os.Process$1.run(Process.java:418)
mUri is content://media/external/file/8
错误发生在以下try/catch
块中:
try {
OutputStream outputStream = mAppCompatActivity.getContentResolver().openOutputStream(Uri.parse(mUri));
outputStream.write(mExtendedEditText.getText().toString().getBytes());
outputStream.close();
//Toast.makeText(mAppCompatActivity, mAppCompatActivity.getString(R.string.file_updated_confirmation), Toast.LENGTH_SHORT).show();
} catch (IOException e){
e.printStackTrace();
dispatchShareEvent();
Toast.makeText(mAppCompatActivity, mAppCompatActivity.getString(R.string.save_failed), Toast.LENGTH_LONG).show();
mExtendedEditText.setText(e.toString() + "\n" + exceptionStacktraceToString(e) + "\n\n" + "mUri is " + mUri);
}
具体来说,这是错误日志中引用的行370
:
OutputStream outputStream = mAppCompatActivity.getContentResolver().openOutputStream(Uri.parse(mUri));
答案 0 :(得分:2)
我认为您只是找到了一个界面来访问我们没有想到的外部文件(或者觉得起作用并不重要)。我们修补文件系统以处理SD卡路径,但是没有对MediaProvider进行任何更改来处理内容Uri,就像您尝试使用的那样。
请随时使用示例代码(或至少链接到此帖子)file a bug,以便我们可以优先处理其他工作。