【问题标题】:File not found Exception in Android but File Exists在 Android 中找不到文件异常但文件存在
【发布时间】:2016-07-28 23:37:44
【问题描述】:

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

我得到文件的位置:

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

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

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

注意:我不是从 sdcard 或资产文件夹或原始文件夹中读取文件,而是在移动 one plus cyanogen 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 读取文件,那么如何将该文件复制到 sdcard 或可以读写的位置

【问题讨论】:

    标签: android android-studio filenotfoundexception


    【解决方案1】:

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

    我没有从 sdcard 读取文件

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


    但是,看到您正在尝试从下载中读取,应该没问题。我认为问题是您的文件路径格式错误:

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

    应该是

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

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

    【讨论】:

    • 问题已解决,只需在 android manifest.xml 中提供适当的 。我现在不记得了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多