2017-01-10 2 views
0

私のアプリを実行するには、携帯電話のメモリに保存したtxtファイルを開いて、そのデータを使用する必要があります。ファイルを開くに失敗しました。マニフェストの権限が完了しました

私はアプリケーションの上のマニフェストに許可を入れて、正しいパスと思われるパスを入れました。プログラムが実行できないとlogcatは

java.io.FileNotFoundException与える:これは、いくつかのあるENOENT(そのようなファイルまたはディレクトリ)

:/storage/TestDaten/input.txt:オープンに失敗しましたがコード。マニフェストに

: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

オープンFi回線で

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


(参考)のAndroid
のSDカードからちょうどあなたのメインの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

提案していただきありがとうございます。しかし、コンテンツを表示する代わりに、ファイルコンテンツを計算に使用したいだけです。私は保存された値を使ってリアルタイム値をシミュレートする必要があります。 –

関連する問題