2017-01-10 1 views
0

내 앱을 실행하려면 휴대 전화의 메모리에 저장 한 txt 파일을 열어서 해당 데이터를 사용해야합니다.파일 열기 실패, 매니페스트 사용 권한 완료

나는 응용 프로그램 위의 매니페스트에 권한을 넣었으며 올바른 경로라고 생각했습니다. 이 프로그램을 실행할 수 없습니다 및 로그 캣은

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

오픈 인터넷

<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> 

는 이제 자바 파일을 열고 코드를 아래에 붙여 넣습니다 :

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(); 
      } 
     } 
} 

지금 실행


그냥 기본 XML 파일을 열고 코드 아래에 붙여 넣기 (참고) 안드로이드
의 SD 카드에서 르 당신의 코드.

P. 코드는 Pdf 파일에 관해서 언급했다.

+0

제안 해 주셔서 감사합니다. 하지만 콘텐츠를 보는 대신 파일 콘텐츠를 계산에 사용하고 싶습니다. 저장된 값을 사용하여 실시간 값을 시뮬레이트해야합니다. –