如何在android中打开文件保存对话框?

问题描述 投票:2回答:3

我有一个Web服务,根据图像ID给我一个byte []数组。我想将这些byte []转换为文件并在android上存储一个文件,用户想要的文件保存文件对话框与文件格式相同。

android android-layout android-widget
3个回答
3
投票

您无法创建保存文件对话框,但您可以通过以下链接将文件从应用程序保存到android sd卡

http://android-er.blogspot.com/2010/07/save-file-to-sd-card.html

http://www.blackmoonit.com/android/filebrowser/intents#intent.pick_file.new


4
投票

Android SDK不提供自己的文件对话框,因此您必须自己构建。


3
投票

由于这是谷歌搜索该主题时的最佳结果,当我研究它时,它让我很困惑,我想我添加了这个问题的更新。从Android 19开始,就有一个内置的保存对话框。你没有事件需要任何权限(甚至不是WRITE_EXTERNAL_STORAGE)。它的工作方式非常简单:

//send an ACTION_CREATE_DOCUMENT intent to the system. It will open a dialog where the user can choose a location and a filename

Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("YOUR FILETYPE"); //not needed, but maybe usefull
intent.putExtra(Intent.EXTRA_TITLE, "YOUR FILENAME"); //not needed, but maybe usefull
startActivityForResult(intent, SOME_INTEGER);

...

//after the user has selected a location you get an uri where you can write your data to:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  if(requestCode == SOME_INTEGER && resultCode == Activity.RESULT_OK) {
    Uri uri = data.getData();

    //just as an example, I am writing a String to the Uri I received from the user:

    try {
      OutputStream output = getContext().getContentResolver().openOutputStream(uri);

      output.write(SOME_CONTENT.getBytes());
      output.flush();
      output.close();
    }
    catch(IOException e) {
      Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show();
    }
  }
}

更多这里:https://developer.android.com/guide/topics/providers/document-provider

© www.soinside.com 2019 - 2024. All rights reserved.