2017-10-01 18 views
0

私がここに投稿している主な理由は、現在、エミュレータを実行できるコンピュータがなくても動かなくなっているため、テストするためにAPKを送信する必要があります。私の電話が私のコンピュータに接続されていたり、サードパーティのエミュレータが動作していても動作しません。このため...エラーログはありません。実行時のパーミッションが原因でクラッシュが発生する

このアプリは単純なパスワードマネージャーで、他のすべての機能はこれまでのところ動作します。私はエクスポート機能を追加しようとしていましたが、実際に何かを書くことはできません。他の質問やさまざまな情報源をオンラインで確認しましたが、何が原因か分かりません。このメソッドが呼び出されると、私が知る限り何もしません。私は何かを逃している場合、または同じ問題で確かに別の質問があった場合はお詫び申し上げます。行方不明のものは見つけられませんでした。

ここに私が使用している方法があります。

EDIT:コードは、最終的にアプリケーションを固定するものであるhere.これを示唆されたランタイム権限を要求するより良い方法を反映するように更新されました。

//Method017: Exports the account info to a .txt file. 
    public void exportData() throws IOException { 

     //Opens dialog to request permission. 
     ActivityCompat.requestPermissions(Main.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); 
    } 

    //Method to handle result of permission request. 
    @Override 
    public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { 
     switch (requestCode) { 
      case 1: { 

       // If request is cancelled, the result arrays are empty. 
       if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

        //Attempt to write a file to the Download folder. 
        String content = "hello world"; 
        File file; 
        FileOutputStream outputStream; 
        try { 
         file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "MyCache"); 

         outputStream = new FileOutputStream(file); 
         outputStream.write(content.getBytes()); 
         outputStream.close(); 

         //According to an online source, this is necessary to make the file viewable on the device. 
         Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
         intent.setData(Uri.fromFile(file)); 
         sendBroadcast(intent); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 

       } else { 

        // permission denied, boo! Disable the 
        // functionality that depends on this permission. 
        Toast.makeText(Main.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show(); 
       } 
       return; 
      } 

      // other 'case' lines to check for other 
      // permissions this app might request 
     } 
    } 

そして、私のマニフェスト:エラーログの欠如について

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.brand.psync"> 

    <application 

     android:allowBackup="true" 
     android:icon="@drawable/psynclogo" 
     android:label="@string/app_name" 
     android:supportsRtl="true" 
     android:theme="@style/AppTheme" 
     android:screenOrientation="portrait"> 
     <activity android:name=".Main"> 
      <intent-filter> 
       <action 
        android:name="android.intent.action.MAIN" 
        android:screenOrientation="portrait" /> 
       <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
       <category 
        android:name="android.intent.category.LAUNCHER" 
        android:screenOrientation="portrait" /> 
      </intent-filter> 
     </activity> 
    </application> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
</manifest> 

申し訳ありません...しかし、私がいることを持っていた場合、私はおそらくここに投稿する必要はありません。

+1

build.gradleファイルで言及した目標となるsdkのバージョンは何ですか? 22を超える場合は、実行時にWRITE_EXTERNAL_STORAGE権限を要求する必要があります。 –

+0

最小値は15ですが、ターゲットは26です。これを24でテストしています。 – Prometheus

+1

スタックトレースを提供できない場合は、Toastを使用してcatchブロックに 'e.getMessage()'と表示し、エラーメッセージを投稿してください。 –

答えて

1

私はあなたのコードを試してみました。

public class SaveFileSampleActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     TextView lblBackground = new TextView(this); 
     lblBackground.setBackgroundColor(Color.WHITE); 
     setContentView(lblBackground); 

     ActivityCompat.requestPermissions(SaveFileSampleActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1); 
    } 


    @Override 
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
     switch (requestCode) { 
      case 1: { 
       // If request is cancelled, the result arrays are empty. 
       if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

        //Attempt to write a file to the Download folder. 
        String content = "hello world"; 
        File file; 
        FileOutputStream outputStream; 
        try { 
         file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "MyCache"); 

         outputStream = new FileOutputStream(file); 
         outputStream.write(content.getBytes()); 
         outputStream.close(); 

         //According to an online source, this is necessary to make the file viewable on the device. 
         Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); 
         intent.setData(Uri.fromFile(file)); 
         sendBroadcast(intent); 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 
       } else { 

        // permission denied, boo! Disable the 
        // functionality that depends on this permission. 
        Toast.makeText(SaveFileSampleActivity.this, "Permission denied to read your External storage", Toast.LENGTH_SHORT).show(); 
       } 
       return; 
      } 
      // other 'case' lines to check for other 
      // permissions this app might request 
     } 
    } 
} 

そしてmainifest

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity 
     android:name=".SaveFileSampleActivity" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

それは仕事との結果です: enter image description here

あなたはあなたのコードを確認することができます。それがあなたを助けることを願っています!

+0

私は混乱しています、あなたが変更したすべてがマニフェストの許可を取り除いていたようですね。私が見る他の違いは、「アクティビティを拡張する」の代わりに「extends AppCompatActivity {」を使用するということです。どのバージョンのアンドロイドをテストしましたか? – Prometheus

+0

まだデバイス上でクラッシュしています... – Prometheus

+0

私はアンドロイドスタジオで作成するエミュレータnexus 5で動作しています。 – Dungnbhut

関連する問題