文件未找到Android中的例外但文件存在

时间:2016-07-28 23:37:45

标签: android android-studio filenotfoundexception

我正在尝试获取android上任何文件的地址,然后阅读其内容。

我将文件的位置设为:

uri2.toString() gives string "file:///storage/emulated/0/download/user.txt"

uri2.getEndodedPath() gives string "/storage/emulated/0/download/user.txt"

将这些值传递给文件时,它会给文件找不到异常

注意:我没有从sdcard或资产文件夹或原始文件夹中读取文件,我正在测试我的应用程序在移动设备上添加氰基mod

这是我的安卓代码

    import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.nfc.Tag;
import android.speech.tts.TextToSpeech;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Locale;


public class MainSpeechActivity extends Activity {

    TextToSpeech tts;
    Button btn;
    private String uri;
    private Uri uri2;

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


        final Intent intent = getIntent();
        final String action = intent.getAction();

        if(Intent.ACTION_VIEW.equals(action)){
            //uri = intent.getStringExtra("URI");
            uri2 = intent.getData();
            uri = uri2.getEncodedPath() + "  complete: " + uri2.toString();
            TextView textView = (TextView)findViewById(R.id.textView);
            TextView textView2 = (TextView)findViewById(R.id.textView2);
            textView.setText(uri);
            // now you call whatever function your app uses

            String str = uri2.toString();// value is mentioned above
            File f = new File(str);
            BufferedReader br = null;
            String line;
            StringBuilder sbr = new StringBuilder();

            try {
                br = new BufferedReader(new InputStreamReader(new FileInputStream(f)));
                while((line = br.readLine()) != null) {
                    sbr.append(line);
                    sbr.append("\n");

                }
                br.close();
            } catch (FileNotFoundException e) {
                Toast.makeText(getApplicationContext(),"FileNotFound",Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            } catch (IOException e) {
                Toast.makeText(getApplicationContext(),"InputError",Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            }
            if(sbr.toString().isEmpty())
                textView2.setText("Blah blah");
            else
            textView2.setText(sbr.toString());




        } else {
            Log.d("know", "intent was something else: " + action);
        }

        tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
            @Override
            public void onInit(int i) {
                if( i != TextToSpeech.ERROR) {
                    tts.setLanguage(Locale.ENGLISH);
                }
            }
        });

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

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String toSpeak = "My friend is very good programmer do you aggree";

                Toast.makeText(getApplicationContext(),toSpeak,Toast.LENGTH_SHORT).show();

                tts.speak(toSpeak,TextToSpeech.QUEUE_FLUSH,null);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main_speech, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

如果无法从android读取文件,那么如何将该文件复制到SD卡或可以读写的位置

1 个答案:

答案 0 :(得分:0)

  

我正在尝试获取android上任何文件的地址,然后阅读其内容。

  

我没有从sdcard读取文件

然后你的选择相当有限。您可以从内部存储中读取文件的唯一方法是允许您的应用访问内部存储的这一部分。

然而,看到你试图从下载中读取,它应该没问题。我认为问题是您的文件路径格式错误:

file:///storage/emulated/0/download/user.txt

应该只是

/storage/emulated/0/download/user.txt

你有价值,你只需要将str变量设置为getEncodedPath而不是toString

相关问题