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

时间:2011-12-21 08:08:17

标签: android android-layout android-widget

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

4 个答案:

答案 0 :(得分:4)

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

答案 1 :(得分: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

答案 2 :(得分:3)

由于这是当您搜索该主题时在Google中排名第一的结果,当我对其进行研究时,它使我感到非常困惑,因此我想我对此问题进行了更新。 自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

答案 3 :(得分:0)

首先,您应该创建一个用于保存文件的对话框意图,在用户选择后,您可以在该目录上写入并指定该文件,而无需任何读/写权限。 (自 Android 19 起)

来源:https://developer.android.com/training/data-storage/shared/documents-files#create-file

   // Request code for creating a PDF document.
    private final int SAVE_DOCUMENT_REQUEST_CODE = 0x445;
    private File targetFile;
 
   private void createFile() {
            Uri reportFileUri = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".provider", targetFile);
        Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        intent.setType("application/pdf");
        intent.putExtra(Intent.EXTRA_TITLE, targetFile.getName());
    
        // Optionally, specify a URI for the directory that should be opened in
        // the system file picker when your app creates the document.
        intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, pickerInitialUri);
        startActivityForResult(intent, SAVE_DOCUMENT_REQUEST_CODE );
    }

   
        @Override
        protected void onActivityResult(int requestCode, int resultCode, @Nullable 
            Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == SAVE_DOCUMENT_REQUEST_CODE && resultCode == RESULT_OK){
                Uri uri = data.getData();
                saveFile(uri);
            }
        }
     
         private void saveFile(Uri uri) {
                try {
                    OutputStream output = getContentResolver().openOutputStream(uri);
                    FileInputStream fileInputStream = new FileInputStream(targetFile);
                    byte[] bytes = new byte[(int) targetFile.length()];
                    fileInputStream.read(bytes, 0, bytes.length);
                    output.write(bytes);
                    output.flush();
                    output.close();
                    Log.i(TAG, "done");
                } catch (IOException e) {
                    Log.e(TAG, "onActivityResult: ", e);
                }
            }