android 手机内存uri_Android-使用意图从手机内存中打开文件

I am working on an app that takes the .txt file from a phone as input and prints it on the TextView,

public class MainActivity extends AppCompatActivity {

Button button;

Intent intent;private StringBuilder text = new StringBuilder();

private InputStream getResources(String s) {

return null;

}

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

button = (Button) findViewById(R.id.btn);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

intent = new Intent(Intent.ACTION_GET_CONTENT);

intent.setType("text/plain");

startActivityForResult(intent, 7);

Log.v("###", "parent " + getParent());

}

});

} @Override

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

String Fpath = data.getDataString();

// final String fileName = ""+Fpath;

Log.v("###", "yo " +Fpath);

super.onActivityResult(requestCode, resultCode, data);

switch (requestCode) {

case 7:

if (resultCode == RESULT_OK) {

setContentView(R.layout.activity_main);

Log.v("###", "hellow");

setContentView(R.layout.activity_main);

BufferedReader reader = null;

try {

reader = new BufferedReader(

new InputStreamReader(getAssets().open("filename.txt")));

//change code

// do reading

String mLine;

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

text.append(mLine);

text.append('\n');

}

} catch (IOException e) {

String PathHolder = data.getData().getPath();

Toast.makeText(MainActivity.this, PathHolder, Toast.LENGTH_LONG).show();

e.printStackTrace();

} finally {

if (reader != null) {

try {

reader.close();

} catch (IOException e) {

//log the exception

}

}

TextView output = (TextView) findViewById(R.id.Txt);

output.setText((CharSequence) text);

}

}

}

}

}

everything is good but the line

new InputStreamReader(getAssets().open("filename.txt")));

Taking the file from Assets folder,

Help me to get the file from My phone,

The file from Assets folder opening properly but I want it to be select from my phone

解决方案

You can get file path in onActivityResult which you selected from file manager.

Uri PathHolder = data.getData();

UPDATE

Above line give you uri of file which you selected from storage. Then You can get File from that Uri easily.

Just forgot about getAssets Use Following method to read from file.

@Override

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

super.onActivityResult(requestCode, resultCode, data);

Uri PathHolder = data.getData();

FileInputStream fileInputStream = null;

StringBuilder text = new StringBuilder();

try {

InputStream inputStream = getContentResolver().openInputStream(PathHolder);

BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));

String mLine;

while ((mLine = r.readLine()) != null) {

text.append(mLine);

text.append('\n');

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值