2016-07-28 69 views
1

我想获取任何文件在Android上的地址,然后阅读其内容。文件未找到在Android中的例外,但文件存在

我收到文件的位置为:

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

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

传递这些价值的文件,它提供了文件未发现异常

时注意:我不是从SD卡或资产文件夹读取文件或原始文件夹,我在移动一加氰国防部

这里测试我的应用程序是我的Android代码

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卡或位置,其中阅读和写作是可以

回答

0

我试图让Android上的任何文件的地址,然后阅读其内容。

我不是从SD卡

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


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

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

应该只是

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

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

+0

str.getEncodedPath()不工作 –

+0

解决的问题只需要在android manifest.xml中给出适当的。我现在不记得了。 –

相关问题