制作简单的文件管理器

时间:2016-12-17 20:58:27

标签: java android android-activity filebrowse

我发现这个代码用于浏览文件夹,但它对我的录音机没用,我想重命名,删除和共享录音机文件夹中的录制文件,但我不知道该怎么做。

import java.io.File;
import java.util.ArrayList;
import android.content.Intent;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import android.util.Log;
import java.util.List;
import java.util.Collections;
import android.widget.ArrayAdapter;
import android.app.*;
import android.os.*;

public class MainActivity extends ListActivity {

private String path;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Use the current directory as title
    path = "/mnt/sdcard/Abzarak/VoiceRecorder";
    if (getIntent().hasExtra("path")) {
        path = getIntent().getStringExtra("path");
    }
    setTitle(path);

    // Read all files sorted into the values-array
    List values = new ArrayList();
    File dir = new File(path);
    if (!dir.canRead()) {
        setTitle(getTitle() + " (inaccessible)");
    }
    String[] list = dir.list();
    if (list != null) {
        for (String file : list) {
            if (!file.startsWith(".")) {
                values.add(file);
            }
        }
    }
    Collections.sort(values);

    // Put the data into the list
    ArrayAdapter adapter = new ArrayAdapter(this,
                                            android.R.layout.simple_list_item_2, android.R.id.text1, values);
    setListAdapter(adapter);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
    String filename = (String) getListAdapter().getItem(position);
    if (path.endsWith(File.separator)) {
        filename = path + filename;
    } else {
        filename = path + File.separator + filename;
    }
    if (new File(filename).isDirectory()) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra("path", filename);
        startActivity(intent);
    } else {
        Toast.makeText(this, filename + " is not a directory", Toast.LENGTH_LONG).show();
    }
}
}

我设法通过添加以下内容打开录制的文件:

import android.net.uri;

并把它放在烤面包上:

} else {
        Intent intent = new Intent();  
        intent.setAction(android.content.Intent.ACTION_VIEW);  
        File file = new File(filename);  
        intent.setDataAndType(Uri.fromFile(file), "audio/*");  
        startActivity(intent);
    }

但我仍然不知道如何删除或重命名或共享录制的文件,我不知道要添加什么。

0 个答案:

没有答案