如何使用 Android Beam 设置发送文件的目录

How to set directory for sending files using Android Beam

我正在开发一款允许用户从外部存储设备 select 文件并使用 Android Beam 发送文件的应用程序。

这里是 FileBrowser Activity 到 select 一个目录中的文件和 return 文件名和目录路径回到 main activity:

public class FileBrowser extends Activity {

    private String root;
    private String currentPath;
    private ArrayList<String> targets;
    private ArrayList<String> paths;
    private File targetFile;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_file_browser);
        getActionBar().setDisplayHomeAsUpEnabled(true);

        root = "/";
        currentPath = root;
        targets = null;
        paths = null;
        targetFile = null;
        showDir(currentPath);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_file_browser, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }


    public void selectDirectory(View view) {
        File f = new File(currentPath);
        targetFile = f;
        //Return target File to activity
        returnTarget();
    }

    public void setCurrentPathText(String message)
    {
        TextView fileTransferStatusText = (TextView) findViewById(R.id.current_path);
        fileTransferStatusText.setText(message);
    }

    private void showDir(String targetDirectory){

        setCurrentPathText("Current Directory: " + currentPath);
        targets = new ArrayList<String>();
        paths = new ArrayList<String>();
        File f = new File(targetDirectory);
        File[] directoryContents = f.listFiles();
        if (!targetDirectory.equals(root))
        {
            targets.add(root);
            paths.add(root);
            targets.add("../");
            paths.add(f.getParent());
        }
        for(File target: directoryContents)
        {
            paths.add(target.getPath());

            if(target.isDirectory())
            {
                 targets.add(target.getName() + "/");
            }
            else
            {
                targets.add(target.getName());

            }

        }

        ListView fileBrowserListView = (ListView) findViewById(R.id.file_browser_listview);

        ArrayAdapter<String> directoryData = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, targets);
        fileBrowserListView.setAdapter(directoryData);
        fileBrowserListView.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View view, int pos,long id) {

                File f = new File(paths.get(pos));

                if(f.isFile())
                {
                    targetFile = f;
                    returnTarget();
                    //Return target File to activity
                }
                else
                {
                    //f must be a dir
                    if(f.canRead())
                    {
                        currentPath = paths.get(pos);
                        showDir(paths.get(pos));
                    }

                }
            }

        });
    }

    public void returnTarget()
    {

        Intent returnIntent = new Intent();
        returnIntent.putExtra("file", targetFile);
        returnIntent.putExtra("path", currentPath);
        setResult(RESULT_OK, returnIntent);
        finish();

    }
}

这是 MainActivity 的代码,其中由 FileBrowser Activity 编辑的文件 return 使用 android beam 发送:

public class MainActivity extends Activity {

    private NfcAdapter nfcAdapter;
    public final int fileRequestID = 98;
    String name;
    String[] extension={".png",".docx",".jpeg",".pdf",".doc"};
    ArrayList <String>supportedExtension=new ArrayList<String>(Arrays.asList(extension));

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        PackageManager pm = this.getPackageManager();
        // Check whether NFC is available on device     
        if (!pm.hasSystemFeature(PackageManager.FEATURE_NFC)) {
            // NFC is not available on the device.
            Toast.makeText(this, "The device does not has NFC hardware.", 
                        Toast.LENGTH_SHORT).show();           
        } 
        // Check whether device is running Android 4.1 or higher 
        else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
            // Android Beam feature is not supported.            
            Toast.makeText(this, "Android Beam is not supported.", 
                        Toast.LENGTH_SHORT).show();                     
        }         
        else {
            // NFC and Android Beam file transfer is supported.         
            Toast.makeText(this, "Android Beam is supported on your device.", 
                        Toast.LENGTH_SHORT).show();
        }
    }
    public void browseForFile(View view) {
        Intent clientStartIntent = new Intent(this, FileBrowser.class);
        startActivityForResult(clientStartIntent, fileRequestID);

    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

         //fileToSend
         boolean filePathProvided;
         File fileToSend;
         if (resultCode == Activity.RESULT_OK && requestCode == fileRequestID) {
            //Fetch result
            File targetDir = (File) data.getExtras().get("file");
            String path = (String)data.getExtras().get("path");
            Log.i("Path=",path);
            if(targetDir.isFile())
            {
                if(targetDir.canRead()) {
                    try{
                        String ext=targetDir.getName().substring(targetDir.getName().lastIndexOf("."));


                        if (supportedExtension.contains(ext)) {
                            fileToSend = targetDir;
                            filePathProvided = true;

                            setTargetFileStatus(targetDir.getName() + " selected for file transfer");
                            Button btn = (Button) findViewById(R.id.send);
                            btn.setVisibility(View.VISIBLE);
                            name = targetDir.getName();

                       }
                       else{
                            Toast.makeText(getApplicationContext(), "File with this extension cannot be printed",
                                Toast.LENGTH_LONG).show();
                        }
                    }catch (Exception e){e.printStackTrace();}
                }
                else
                {
                    filePathProvided = false;
                    setTargetFileStatus("You do not have permission to read the file " + targetDir.getName());
                }

            }
            else
            {
                filePathProvided = false;
                setTargetFileStatus("You may not transfer a directory, please select a single file");
            }

        }
    }

    public void setTargetFileStatus(String message)
    {
        TextView targetFileStatus = (TextView) findViewById(R.id.selected_filename);
        targetFileStatus.setText(message);
    }
    public void sendFile(View view) {
        nfcAdapter = NfcAdapter.getDefaultAdapter(this);  

        // Check whether NFC is enabled on device
        if(!nfcAdapter.isEnabled()){
            Toast.makeText(this, "Please enable NFC.", Toast.LENGTH_SHORT).show(); 
            startActivity(new Intent(Settings.ACTION_NFC_SETTINGS));
        }        

        else if(!nfcAdapter.isNdefPushEnabled()) { 
            Toast.makeText(this, "Please enable Android Beam.", 
                        Toast.LENGTH_SHORT).show();
            startActivity(new Intent(Settings.ACTION_NFCSHARING_SETTINGS));
        }
        else {    
            Uri[] mFileUris = new Uri[1];
            String fileName=name;
            // Retrieve the path to the user's public pictures directory 
            File fileDirectory = Environment
                                .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
            File fileToTransfer;
            fileToTransfer = new File(fileDirectory, fileName);
            fileToTransfer.setReadable(true, false);
            mFileUris[0] = Uri.fromFile(fileToTransfer);
            nfcAdapter.setBeamPushUris(mFileUris, this);
        }
    }

}

现在,正如您在我的 MainActivity 中看到的那样,我将我的目录设置为图片。

File fileDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);                         

我的问题是如何根据从 FileBrowser 获得的实际目录值每次动态更改我的目录Activity?

我已经阅读了 How to send files using Android Beam, but didn't find it much useful for my problem. I also went through the android documentation of Environment 的 android 文档,但不太了解。

任何有关此的帮助将不胜感激。提前致谢!

您已经在 OnActivityResult 方法中选择了文件。只是改变

mFileUris[0] = Uri.fromFile(fileToTransfer);

mFileUris[0] = Uri.fromFile(targetDir);