如何在Android应用程序中打开文件?

时间:2014-08-20 14:37:29

标签: android file android-intent android-activity

我正在尝试创建一个可以从手机目录中打开文件的应用。我将打开.ddd文件,但希望能够打开任何文件类型。我知道可以使用意图。我已经尝试了这个,但目前它只是打开进入文件选择但不打开文件。

import java.io.File;
import java.io.Serializable;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import ar.com.daidalos.afiledialog.*;

public class AFileDialogTestingActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Assign behaviors to the buttons.
        Button buttonActivity1 = (Button)this.findViewById(R.id.activity_simple_open);
        buttonActivity1.setOnClickListener(btnActivitySimpleOpen);
        Button buttonActivity2 = (Button)this.findViewById(R.id.activity_open_downloads);
        buttonActivity2.setOnClickListener(btnActivityOpenDownloads);
        Button buttonActivity3 = (Button)this.findViewById(R.id.activity_select_folders);
        buttonActivity3.setOnClickListener(btnActivitySelectFolders);
        Button buttonActivity4 = (Button)this.findViewById(R.id.activity_create_files);
        buttonActivity4.setOnClickListener(btnActivityCreateFiles);
        Button buttonActivity5 = (Button)this.findViewById(R.id.activity_select_images);
        buttonActivity5.setOnClickListener(btnActivitySelectImages);
        Button buttonActivity6 = (Button)this.findViewById(R.id.activity_ask_confirmation);
        buttonActivity6.setOnClickListener(btnActivityAskConfirmation);
        Button buttonActivity7 = (Button)this.findViewById(R.id.activity_custom_labels);
        buttonActivity7.setOnClickListener(btnActivityCustomLabels);

        Button buttonDialog1 = (Button)this.findViewById(R.id.dialog_simple_open);
        buttonDialog1.setOnClickListener(btnDialogSimpleOpen);
        Button buttonDialog2 = (Button)this.findViewById(R.id.dialog_open_downloads);
        buttonDialog2.setOnClickListener(btnDialogOpenDownloads);
        Button buttonDialog3 = (Button)this.findViewById(R.id.dialog_select_folders);
        buttonDialog3.setOnClickListener(btnDialogSelectFolders);
        Button buttonDialog4 = (Button)this.findViewById(R.id.dialog_create_files);
        buttonDialog4.setOnClickListener(btnDialogCreateFiles);
        Button buttonDialog5 = (Button)this.findViewById(R.id.dialog_select_images);
        buttonDialog5.setOnClickListener(btnDialogSelectImages);
        Button buttonDialog6 = (Button)this.findViewById(R.id.dialog_ask_confirmation);
        buttonDialog6.setOnClickListener(btnDialogAskConfirmation);
        Button buttonDialog7 = (Button)this.findViewById(R.id.dialog_custom_labels);
        buttonDialog7.setOnClickListener(btnDialogCustomLabels);
    }

    // ----- Buttons for open a dialog ----- //

    private OnClickListener btnDialogSimpleOpen = new OnClickListener() {
        public void onClick(View v) {
            // Create the dialog.
            FileChooserDialog dialog = new FileChooserDialog(AFileDialogTestingActivity.this);

            // Assign listener for the select event.
            dialog.addListener(AFileDialogTestingActivity.this.onFileSelectedListener);

            // Show the dialog.
            dialog.show();
        }
    };    

    private OnClickListener btnDialogOpenDownloads = new OnClickListener() {
        public void onClick(View v) {
            // Create the dialog.
            FileChooserDialog dialog = new FileChooserDialog(AFileDialogTestingActivity.this);

            // Assign listener for the select event.
            dialog.addListener(AFileDialogTestingActivity.this.onFileSelectedListener);

            // Define start folder.
            dialog.loadFolder(Environment.getExternalStorageDirectory() + "/Download/");

            // Show the dialog.
            dialog.show();
        }
    };    

    private OnClickListener btnDialogSelectFolders = new OnClickListener() {
        public void onClick(View v) {
            // Create the dialog.
            FileChooserDialog dialog = new FileChooserDialog(AFileDialogTestingActivity.this);

            // Assign listener for the select event.
            dialog.addListener(AFileDialogTestingActivity.this.onFileSelectedListener);

            // Activate the folder mode.
            dialog.setFolderMode(true);

            // Show the dialog.
            dialog.show();
        }
    };    

    private OnClickListener btnDialogCreateFiles = new OnClickListener() {
        public void onClick(View v) {
            // Create the dialog.
            FileChooserDialog dialog = new FileChooserDialog(AFileDialogTestingActivity.this);

            // Assign listener for the select event.
            dialog.addListener(AFileDialogTestingActivity.this.onFileSelectedListener);

            // Activate the button for create files.
            dialog.setCanCreateFiles(true);

            // Show the dialog.
            dialog.show();
        }
    };  

    private OnClickListener btnDialogSelectImages = new OnClickListener() {
        public void onClick(View v) {
            // Create the dialog.
            FileChooserDialog dialog = new FileChooserDialog(AFileDialogTestingActivity.this);

            // Assign listener for the select event.
            dialog.addListener(AFileDialogTestingActivity.this.onFileSelectedListener);

            // Define the filter for select images.
            dialog.setFilter(".*jpg|.*png|.*gif|.*JPG|.*PNG|.*GIF");
            dialog.setShowOnlySelectable(false);

            // Show the dialog.
            dialog.show();
        }
    };

    private OnClickListener btnDialogAskConfirmation = new OnClickListener() {
        public void onClick(View v) {
            // Create the dialog.
            FileChooserDialog dialog = new FileChooserDialog(AFileDialogTestingActivity.this);

            // Assign listener for the select event.
            dialog.addListener(AFileDialogTestingActivity.this.onFileSelectedListener);

            // Activate the button for create files.
            dialog.setCanCreateFiles(true);

            // Activate the confirmation dialogs.
            dialog.setShowConfirmation(true, true);

            // Show the dialog.
            dialog.show();
        }
    };

    private OnClickListener btnDialogCustomLabels = new OnClickListener() {
        public void onClick(View v) {
            // Create the dialog.
            FileChooserDialog dialog = new FileChooserDialog(AFileDialogTestingActivity.this);

            // Assign listener for the select event.
            dialog.addListener(AFileDialogTestingActivity.this.onFileSelectedListener);

            // Activate the folder mode.
            dialog.setFolderMode(true);

            // Activate the button for create files.
            dialog.setCanCreateFiles(true);

            // Activate the confirmation dialogs.
            dialog.setShowConfirmation(true, true);

            // Define the labels.
            FileChooserLabels labels = new FileChooserLabels();
            labels.createFileDialogAcceptButton = "AcceptButton";
            labels.createFileDialogCancelButton = "CancelButton";
            labels.createFileDialogMessage = "DialogMessage";
            labels.createFileDialogTitle = "DialogTitle";
            labels.labelAddButton = "AddButton";
            labels.labelSelectButton = "SelectButton";
            labels.messageConfirmCreation = "messageConfirmCreation";
            labels.messageConfirmSelection = "messageConfirmSelection";
            labels.labelConfirmYesButton = "yesButton";
            labels.labelConfirmNoButton = "noButton";
            dialog.setLabels(labels);

            // Show the dialog.
            dialog.show();
        }
    };

    // ---- Buttons for open an activity ----- //

    private OnClickListener btnActivitySimpleOpen = new OnClickListener() {
        public void onClick(View v) {
            // Create the intent for call the activity.
            Intent intent = new Intent(AFileDialogTestingActivity.this, FileChooserActivity.class);

            // Call the activity            
            AFileDialogTestingActivity.this.startActivityForResult(intent, 0);
        }
    };    

    private OnClickListener btnActivityOpenDownloads = new OnClickListener() {
        public void onClick(View v) {
            // Create the intent for call the activity.
            Intent intent = new Intent(AFileDialogTestingActivity.this, FileChooserActivity.class);

            // Define start folder.
            intent.putExtra(FileChooserActivity.INPUT_START_FOLDER, Environment.getExternalStorageDirectory() + "/Download/");

            // Call the activity            
            AFileDialogTestingActivity.this.startActivityForResult(intent, 0);          
        }
    };    

    private OnClickListener btnActivitySelectFolders = new OnClickListener() {
        public void onClick(View v) {
            // Create the intent for call the activity.
            Intent intent = new Intent(AFileDialogTestingActivity.this, FileChooserActivity.class);

            // Activate the folder mode.
            intent.putExtra(FileChooserActivity.INPUT_FOLDER_MODE, true);

            // Call the activity            
            AFileDialogTestingActivity.this.startActivityForResult(intent, 0);  
        }
    };    

    private OnClickListener btnActivityCreateFiles = new OnClickListener() {
        public void onClick(View v) {
            // Create the intent for call the activity.
            Intent intent = new Intent(AFileDialogTestingActivity.this, FileChooserActivity.class);

            // Activate the button for create files.
            intent.putExtra(FileChooserActivity.INPUT_CAN_CREATE_FILES, true);

            // Call the activity            
            AFileDialogTestingActivity.this.startActivityForResult(intent, 0);  
        }
    };  

    private OnClickListener btnActivitySelectImages = new OnClickListener() {
        public void onClick(View v) {
            // Create the intent for call the activity.
            Intent intent = new Intent(AFileDialogTestingActivity.this, FileChooserActivity.class);

            // Define the filter for select images.
            intent.putExtra(FileChooserActivity.INPUT_REGEX_FILTER, ".*jpg|.*png|.*gif|.*JPG|.*PNG|.*GIF");

            // Call the activity            
            AFileDialogTestingActivity.this.startActivityForResult(intent, 0);  
        }
    };

    private OnClickListener btnActivityAskConfirmation = new OnClickListener() {
        public void onClick(View v) {
            // Create the intent for call the activity.
            Intent intent = new Intent(AFileDialogTestingActivity.this, FileChooserActivity.class);

            // Activate the button for create files.
            intent.putExtra(FileChooserActivity.INPUT_CAN_CREATE_FILES, true);

            // Activate the confirmation dialogs.
            intent.putExtra(FileChooserActivity.INPUT_SHOW_CONFIRMATION_ON_CREATE, true);
            intent.putExtra(FileChooserActivity.INPUT_SHOW_CONFIRMATION_ON_SELECT, true);

            // Call the activity            
            AFileDialogTestingActivity.this.startActivityForResult(intent, 0); 
        }
    };

    private OnClickListener btnActivityCustomLabels = new OnClickListener() {
        public void onClick(View v) {
            // Create the intent for call the activity.
            Intent intent = new Intent(AFileDialogTestingActivity.this, FileChooserActivity.class);

            // Activate the folder mode.
            intent.putExtra(FileChooserActivity.INPUT_FOLDER_MODE, true);

            // Activate the button for create files.
            intent.putExtra(FileChooserActivity.INPUT_CAN_CREATE_FILES, true);

            // Activate the confirmation dialogs.
            intent.putExtra(FileChooserActivity.INPUT_SHOW_CONFIRMATION_ON_CREATE, true);
            intent.putExtra(FileChooserActivity.INPUT_SHOW_CONFIRMATION_ON_SELECT, true);

            // Define the labels.
            FileChooserLabels labels = new FileChooserLabels();
            labels.createFileDialogAcceptButton = "AcceptButton";
            labels.createFileDialogCancelButton = "CancelButton";
            labels.createFileDialogMessage = "DialogMessage";
            labels.createFileDialogTitle = "DialogTitle";
            labels.labelAddButton = "AddButton";
            labels.labelSelectButton = "SelectButton";
            labels.messageConfirmCreation = "messageConfirmCreation";
            labels.messageConfirmSelection = "messageConfirmSelection";
            labels.labelConfirmYesButton = "yesButton";
            labels.labelConfirmNoButton = "noButton";
            intent.putExtra(FileChooserActivity.INPUT_LABELS, (Serializable) labels);

            // Call the activity            
            AFileDialogTestingActivity.this.startActivityForResult(intent, 0); 
        }
    };

    private OnClickListener clickButtonOpenActivity = new OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(AFileDialogTestingActivity.this, FileChooserActivity.class);
            intent.putExtra(FileChooserActivity.INPUT_REGEX_FILTER, ".*pdf|.*jpg|.*png|.*mp3|.*mp4|.*avi");           
            intent.putExtra(FileChooserActivity.INPUT_SHOW_ONLY_SELECTABLE, true);           
            intent.putExtra(FileChooserActivity.INPUT_CAN_CREATE_FILES, true);
            intent.putExtra(FileChooserActivity.INPUT_FOLDER_MODE, true);
            intent.putExtra(FileChooserActivity.INPUT_SHOW_CONFIRMATION_ON_CREATE, true);
            intent.putExtra(FileChooserActivity.INPUT_SHOW_CONFIRMATION_ON_SELECT, true);

            // Define labels.
            FileChooserLabels labels = new FileChooserLabels();
            labels.createFileDialogAcceptButton = "AcceptButton";
            labels.createFileDialogCancelButton = "CancelButton";
            labels.createFileDialogMessage = "DialogMessage";
            labels.createFileDialogTitle = "DialogTitle";
            labels.labelAddButton = "AddButton";
            labels.labelSelectButton = "SelectButton";
            labels.messageConfirmCreation = "messageConfirmCreation";
            labels.messageConfirmSelection = "messageConfirmSelection";
            labels.labelConfirmYesButton = "yesButton";
            labels.labelConfirmNoButton = "noButton";
            intent.putExtra(FileChooserActivity.INPUT_LABELS, (Serializable) labels);   

            AFileDialogTestingActivity.this.startActivityForResult(intent, 0);
        }
    };

    // ---- Methods for display the results ----- //

    private FileChooserDialog.OnFileSelectedListener onFileSelectedListener = new FileChooserDialog.OnFileSelectedListener() {
        public void onFileSelected(Dialog source, File file) {
            source.hide();
            Toast toast = Toast.makeText(AFileDialogTestingActivity.this, "File selected: " + file.getName(), Toast.LENGTH_LONG);
            toast.show();
        }
        public void onFileSelected(Dialog source, File folder, String name) {
            source.hide();
            Toast toast = Toast.makeText(AFileDialogTestingActivity.this, "File created: " + folder.getName() + "/" + name, Toast.LENGTH_LONG);
            toast.show();
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            boolean fileCreated = false;
            String filePath = "";

            Bundle bundle = data.getExtras();
            if(bundle != null)
            {
                if(bundle.containsKey(FileChooserActivity.OUTPUT_NEW_FILE_NAME)) {
                    fileCreated = true;
                    File folder = (File) bundle.get(FileChooserActivity.OUTPUT_FILE_OBJECT);
                    String name = bundle.getString(FileChooserActivity.OUTPUT_NEW_FILE_NAME);
                    filePath = folder.getAbsolutePath() + "/" + name;
                } else {
                    fileCreated = false;
                    File file = (File) bundle.get(FileChooserActivity.OUTPUT_FILE_OBJECT);
                    filePath = file.getAbsolutePath();
                }
            }

            String message = fileCreated? "File created" : "File opened";
            message += ": " + filePath;
            Toast toast = Toast.makeText(AFileDialogTestingActivity.this, message, Toast.LENGTH_LONG);
            toast.show();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我就是这样做的。您还应该确保在AndroidManifest中包含<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

xml文件

 Button
        android:id="@+id/FileButtonOrWhatever"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:onClick="GetFiles" 

java文件

 public void GetFiles(View view) {
    // get the files directory
    File lister = this.getFilesDir();

    FileInputStream inputStream = null;

    byte[] bytes = new byte[500];

    int fileIdx = -1;

    for (String list : lister.list()){
        fileIdx++;
        if(list.endsWith("ddd")){
            File file = lister.listFiles()[fileIdx];
            try {
               inputStream = new FileInputStream(file);
               bytes = new byte[inputStream.available()];
               inputStream.read(bytes);
            } catch (FileNotFoundException e) {
               e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();}
            finally {
                if (inputStream != null) {
                   try {
                       inputStream.close();
                   } catch (IOException e) {
                       e.printStackTrace();
                   }
                }
            }
        }
    }
}