2017-01-10 58 views
0

爲了讓我的應用程序運行,它需要打開一個txt文件,我將它保存到手機內存中,並使用其中的數據。打開文件失敗,完成清單中的權限

我已經將清單中的權限放在應用程序之上,並且已經把我認爲是正確的路徑。該程序無法運行,並給出了logcat中

java.io.FileNotFoundException:/storage/TestDaten/input.txt:打開失敗:ENOENT(沒有這樣的文件或目錄)

這是一些碼。

在清單:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE_"></uses-permission>

在MainActivity

public class MainActivity extends AppCompatActivity { 
 

 

 
    // Storage Permissions 
 
    private static final int REQUEST_EXTERNAL_STORAGE = 1; 
 
    private static String[] PERMISSIONS_STORAGE = { 
 
      // Manifest.permission.READ_EXTERNAL_STORAGE, 
 
      Manifest.permission.WRITE_EXTERNAL_STORAGE 
 
    }; 
 

 
    public static void verifyStoragePermissions(Activity activity) { 
 
     // Check if we have write permission 
 
     int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE); 
 

 
     if (permission != PackageManager.PERMISSION_GRANTED) { 
 
      // We don't have permission so prompt the user 
 
      ActivityCompat.requestPermissions(
 
        activity, 
 
        PERMISSIONS_STORAGE, 
 
        REQUEST_EXTERNAL_STORAGE 
 
      ); 
 
     } 
 
    } 
 
AudioAnalyzer aa = new AudioAnalyzer(); 
 
    Button button; 
 

 

 
    private View.OnClickListener runfilter = new View.OnClickListener() { 
 
     @Override 
 
     public void onClick(View v) { 
 

 
      try { 
 
       double[] input = readFile("/storage/TestDaten/input.txt"); 
 
       
 
       int i; 
 
       double inputd = input[0]; 
 
       for (i=0;i<input.length-1; i++){ 
 
        inputd = input[i]; 
 
       } 
 

 

 
       isDetected(aa.EventAnalyzer(inputd)); 
 
      }catch (IOException e) { 
 
       e.printStackTrace(); 
 
      } 
 

 
     } 
 
    }; 
 
@Override 
 
    protected void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 
     setContentView(R.layout.activity_main); 
 
     button = (Button) findViewById(R.id.buttonrun); 
 
     button.setOnClickListener(runfilter); 
 
     
 
     verifyStoragePermissions(this); 
 
    } 
 

 
    public double[] readFile(String fname) throws FileNotFoundException{ 
 
     File file = new File(fname); 
 
     Scanner scanner = new Scanner(new FileInputStream(file)); 
 
     List<Double> collection = new ArrayList<>(); 
 

 
     while (scanner.hasNextDouble()) 
 
     { 
 
      double number = scanner.nextDouble(); 
 
      collection.add(number); 
 
     } 
 

 
     scanner.close(); 
 
     return toPrimitive(collection.toArray(new Double[collection.size()])); 
 
    } 
 
public double[] toPrimitive(Double[] array) { 
 
     if (array == null) { 
 
      return null; 
 
     } else if (array.length == 0) { 
 
      return null; 
 
     } 
 
     final double[] result = new double[array.length]; 
 
     for (int i = 0; i < array.length; i++) { 
 
      result[i] = array[i].doubleValue(); 
 
     } 
 
     return result;} 
 

 

 
    private void isDetected(int a){ 
 

 

 
     Context context = getApplicationContext(); 
 
     CharSequence t0 = "No breathing"; 
 
     CharSequence t1 = "Normal breathing"; 
 
     CharSequence t2 = "Snoring"; 
 
     CharSequence t3 = "Apnea!"; 
 
     int duration = Toast.LENGTH_SHORT; 
 
     Toast toast0 = Toast.makeText(context, t0, duration); 
 
     Toast toast1 = Toast.makeText(context, t1, duration); 
 
     Toast toast2 = Toast.makeText(context, t2, duration); 
 
     Toast toast3 = Toast.makeText(context, t3, duration); 
 

 
     if(a==0) 
 
      toast0.show(); 
 
     if(a==1) 
 
      toast1.show(); 
 
     if(a==2) 
 
      toast2.show(); 
 
     if(a==3) 
 
      toast3.show(); 
 

 
    } 
 
}

回答

0

打開網絡連接樂從SD卡中的Android(僅供參考)
只要打開你的主XML文件,並粘貼如下代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/ll" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="My BOOK" 
     android:onClick="book"/> 

</LinearLayout> 

現在打開你的java文件,並粘貼如下代碼:

package sel.listG; //package name 

import java.io.File; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Toast; 
import android.app.Activity; 
import android.content.ActivityNotFoundException; 
import android.content.Intent; 

public class MainActivity extends Activity { 


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


     } 
     public void book(View v) 
     { 
//your pdf file path 
      File file=new File("/sdcard/xxxx.pdf"); 
      if(file.exists()) 
      { 
        Intent i= new Intent(Intent.ACTION_VIEW); 
//set pdf to doc if you want to open doc 
        i.setDataAndType(Uri.fromFile(file), "application/pdf"); 
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        try 
        { 
        startActivity(i); 
        } 
        catch(ActivityNotFoundException e) 
        { 
//if pdf reader not found than open browser to download pdf reader 
         Intent i1=new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=com.adobe.reader&hl=en")); 
         startActivity(i1); 
        } 
      } 
      else 
      { 
//if pdf not found on given location 
        Toast.makeText(getApplicationContext(), "xxxx.pdf not found", Toast.LENGTH_LONG).show(); 
      } 
     } 
} 

現在運行你的代碼。

P.S.該代碼表示​​關於PDF文件。

+0

感謝您的建議。但不是查看內容,我只是想使用文件內容進行計算。我需要使用保存的值來模擬實時值。 –

相關問題