2016-07-28 90 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中給出適當的。我現在不記得了。 –

相關問題