2017-07-06 9 views
0

私はSDCard Documentsフォルダのサブフォルダにいくつかのファイルを保存します。私は多くの記事を読んだことがありますが、最終的には例外がありません。ファイルストアはうまくいくようですが、電話をcompに接続すると、フォルダやファイルが表示されません。android marshmallow sdcardに公開ファイルを書く

EDIT

このファイルが

を作成している私はからの書き込み許可を求めてきました

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

マニフェストファイルにアクセス権を付与されてきた、エミュレータ上で、唯一のデバイス上で発生そのユーザーは、許可されます。このポスト This post helped me in granting permission

は、ここに私のコードの任意のヒントを歓迎している

if (!Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED)) { 
     Toast.makeText(_ctx, "There is no external storage", Toast.LENGTH_LONG).show(); 
    } else { 
     File docfolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS); 
     docfolder.mkdirs(); 
     if (!docfolder.exists()) { 
      Toast.makeText(_ctx, "Documents doesn't exist", Toast.LENGTH_LONG).show(); 
     } 
     File folder = new File(docfolder, foldername); 
     folder.mkdirs(); //make if not exist 

     boolean isPresent = folder.exists(); 
     if (isPresent) { 
      File file = new File(folder.getAbsolutePath(), filename); 

      try { 
       OutputStream os = new FileOutputStream(file); 
       os.write(xml.getBytes()); 
       os.close(); 
      } catch (IOException e) { 
       Toast.makeText(_ctx, e.toString(), Toast.LENGTH_LONG).show(); 

       Log.w("ExternalStorage", "Error writing " + file, e); 
      } 
      catch (Exception e) { 
       Toast.makeText(_ctx, e.toString(), Toast.LENGTH_LONG).show(); 

       Log.w("ExternalStorage", "Error writing 2 " + file, e); 
      } 
     } else { 
      Toast.makeText(_ctx, "cannot make directory " + foldername, Toast.LENGTH_LONG).show(); 
      Log.w("ExternalStorage", "cannot make directory " + foldername); 
     } 

です!

EDIT

多分問題は、このコードは、そのパスにファイルを保存していることだろうか?私はEnvironment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)メソッドを使用しているため/storage/emulated/0/Documents/IBHTrack/ibhTrack_GPX_20170706153858.xml

しかし、私は

+0

https://stackoverflow.com/questions/32789157/how-to-write-files-to-external-public-storage-in-android -the-their-are-visibl – CommonsWare

+0

[Androidの外部パブリックストレージにファイルを書き込んでWindowsから見えるようにする方法](https://stackoverflow.com/questions/32789157/how-to) -write-files-to-external-public-storage-in-and-so-are-visibl) – Sanoop

+0

ありがとうございますが、mediascannerが私を助けなかったようです。 – pillesoft

答えて

0

はまず権限を確認し、実際のSDカードのパスを取得する方法がわかりません。

@Override 
    public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) { 
     switch (requestCode) { 
      case 101: { 
       // If request is cancelled, the result arrays are empty. 
       if (grantResults.length > 0 
         && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
        processFileLog(messageForLog); 
       } else { 
        // permission denied, boo! Disable the 
        // functionality that depends on this permission. 
        Toast.makeText(MainActivity.this,"Permission denied, now log will not write in storage.",Toast.LENGTH_SHORT).show(); 

       } 
       return; 
      } 

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

void checkPermission(Activity thisActivity){ 
     // Here, thisActivity is the current activity 
     if (ContextCompat.checkSelfPermission(thisActivity, 
       Manifest.permission.WRITE_EXTERNAL_STORAGE) 
       != PackageManager.PERMISSION_GRANTED) { 
      // Should we show an explanation? 
      if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, 
        Manifest.permission.WRITE_EXTERNAL_STORAGE)) { 
       // Show an explanation to the user *asynchronously* -- don't block 
       // this thread waiting for the user's response! After the user 
       // sees the explanation, try again to request the permission. 
       Toast.makeText(MainActivity.this,"Permission denied, now log will not write in storage.",Toast.LENGTH_SHORT).show(); 

      } else { 
       // No explanation needed, we can request the permission. 
       ActivityCompat.requestPermissions(thisActivity, 
         new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 
         101); 

      } 
     }else { 
       processFileLog(messageForLog); 

     } 
    } 

次に許可の応答を処理ファイルにデータを記録することができ、この方法では

/** 
    * Tag value 
    */ 
    private static final String mTag = "Logs"; 

    /** 
    * File 
    */ 
    private static File mLogFolder = null; 

    private static void writeLog() { 
     if (isExternalStorageWritable()) { 
      mLogFolder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + 
        "/app-client-logs"); 
      if (!mLogFolder.exists()) { 
       if (!mLogFolder.mkdirs()) { 
        System.out.println("Unable to create the log folder"); 
       } 
      } 
     } else { 
      System.out.println("SD Card not available for read and write"); 
     } 
    } 

    public static void processFileLog(final String message) { 
     writeLog(); 
     File file = new File(mLogFolder, mTag + ".log"); 
     try { 
      BufferedWriter buf = new BufferedWriter(new FileWriter(file, true)); 
      String currentDateTimeString = DateFormat.getDateTimeInstance().format(System.currentTimeMillis()); 
      buf.append("\nSafety Net Request "+currentDateTimeString).append(":\n").append(message); 
      buf.newLine(); 
      buf.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
+0

ありがとうございますが、私の場合でも権限がチェックされています – pillesoft

関連する問題