【问题标题】:import file to android with a file exporer使用文件资源管理器将文件导入到 android
【发布时间】:2013-10-11 08:07:59
【问题描述】:

我已经四处搜索,但找不到适合我的问题的解决方案。我对 Android 很陌生。

构建一个可以读取和编辑 csv 文件的应用。

到目前为止,我的应用程序从资产文件夹中读取 csv。这是我使用的代码。 Import.java 代码。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;


public class CSVAdapter extends ArrayAdapter<State>{
Context ctx;


public CSVAdapter(Context context, int textViewResourceId) {
    super(context, textViewResourceId);


    this.ctx = context;

    //Load the data.
    loadArrayFromFile();    
}




@Override
public View getView(final int pos, View convertView, final ViewGroup parent){

    TextView mView = (TextView)convertView;

    if(null == mView){ 
        mView = new TextView(parent.getContext());
        mView.setTextSize(28);
    }


    mView.setText(getItem(pos).getName());



    /*mView.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            Toast.makeText(parent.getContext(), getItem(pos).getCapital(), Toast.LENGTH_SHORT).show();
        }
    });*/

    return mView;
}

private void loadArrayFromFile(){
    try {

        InputStream is = ctx.getAssets().open("states.csv"); 
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        String line;


        while ((line = reader.readLine()) != null) {

            String[] RowData = line.split(",");


            State cur = new State();
            cur.setName(RowData[0]);
            cur.setCapital(RowData[1]);


            this.add(cur);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}


}

如您所见,inputStream 位于 assets 文件夹中。我希望能够在外部存储中搜索 csv 文件,例如下载或 SD 卡上,但不知道如何打开文件浏览器来查找它。

有谁能帮忙吗?

谢谢!

【问题讨论】:

  • Android 没有内置的文件浏览器。因此,您要么必须编写一个,包含其他人提供的一个,要么让用户单独安装一个并通过 Intent 触发它。
  • @user2339071 - 不,这似乎不是重复的,因为它只涵盖了更复杂任务中最琐碎的部分。
  • 哦.. 我的错.. :P .. 删除它。

标签: java android csv


【解决方案1】:

这里有一个更大的概述:

http://developer.android.com/guide/topics/data/data-storage.html

但是:


内部存储访问:

String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

外部存储访问:

File file = new File(getExternalFilesDir(null), "DemoFile.jpg");

【讨论】:

  • 这似乎并没有真正回答有关文件浏览的问题。
猜你喜欢
  • 2011-09-14
  • 2013-01-10
  • 1970-01-01
  • 1970-01-01
  • 2015-05-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多