2016-09-20 7 views
0

基本的にはmkdirでフォルダを作成し、そのフォルダに単純なテキストファイルを作成します。最後のステップとして、MediaScannerConnection.ScanFileを使用して、ファイルエクスプローラでそれらを表示させます。Android API 23 mkdirが作成したフォルダはWindowsエクスプローラで1つのファイルとして表示されます

フォルダーとテキストファイルは、私のアンドロイドphone.When私のWindowsエクスプローラーに私のファイルエクスプローラで表示されますMTPを使用してUSB経由で電話を接続すると、私は新しく作成されたフォルダは、ファイル、拡張子なし、4KBサイズ

Smartphoneとコンピュータを再起動しても問題は解決しませんが、アンドロイドファイルエクスプローラでフォルダの名前を変更してから電話を再起動すると、Windowsエクスプローラの通常のフォルダとして表示されます。

Appは、ボタンをクリックした後、上書きするファイルのリスト、新しいファイル名を入力するedittext、否定的なボタン、ポジティブボタンを含むダイアログを表示します。ここで

コード:

/* 
Save Config in a File stored in interal storage --> which is emulated as external storage 0 
Shows a Dialog window with the option to select a file which is already there or type in a name for a new file or choose from a list of files 
--> save the new file or cancel the dialog 
*/ 
public void savebutton(View view) { 

     try { 
      // Instantiate an AlertDialog with application context this 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      //External Storage storage/emulated/0/Config --> storage/emulated/0 is internal storage emulated as sd-card 
      dir = new File(Environment.getExternalStorageDirectory() + folder); 
      if (!dir.exists()) { 
       if (!dir.mkdir()) { //create directory if not existing yet 
        Log.d(MainActivity.LOG_TAG, "savebutton: directory was not created"); 
       } 
      } 

      //set title of dialog 
      builder.setTitle("Save Configuration in Text File"); 
      //Add edittext to dialog 
      final EditText input = new EditText(ConfigActivity.this); 
      LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT, 
        LinearLayout.LayoutParams.MATCH_PARENT); 
      input.setLayoutParams(lp); 
      input.setImeOptions(EditorInfo.IME_ACTION_DONE);  // instead of a RETURN button you get a DONE button 
      input.setSingleLine(true);        // single line cause more lines would change the layout 
      java.util.Calendar c = java.util.Calendar.getInstance(); 
      int day = c.get(java.util.Calendar.DAY_OF_MONTH); 
      int month = c.get(java.util.Calendar.MONTH) + 1; 
      int year = c.get(java.util.Calendar.YEAR); 
      String date = Integer.toString(day) + "." + Integer.toString(month) + "." + Integer.toString(year); 
      String savename = name + "-" + date; 
      input.setText(savename); 

      //Append term + "=" + value --> like LevelOn=50 
      for (int itemcounter = 0; itemcounter < value.length; itemcounter++) 
       datastring += (term[itemcounter] + "=" + getvalue(itemcounter) + "\n"); 

      //File List of already stored files for dialog 
      mFilelist = dir.list(); 
      builder.setItems(mFilelist, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 

        mChosenFile = mFilelist[which]; // the item/file which was selected in the file list 

        try { 
         //Create text file in directory /Pump Config Files 
         File file = new File(dir.getAbsolutePath(), mChosenFile); 
         //create bufferedwrite with filewriter 
         BufferedWriter bw = new BufferedWriter(new FileWriter(file)); 
         //write to file --> an existing file will be overwritten 
         bw.write(datastring); 
         //Flush bufferedwriter 
         bw.close(); 
         //LOG 
         System.out.println("Wrote to file"); 
         //Show a message 
         Toast.makeText(getApplicationContext(), "Saved data", Toast.LENGTH_SHORT).show(); 

         // initiate media scan -->cause sometimes created files are nt shown on computer when connected to phone via USB 
         // restarting the smartphone or rename folder can help too 
         // make the scanner aware of the location and the files you want to see 
         MediaScannerConnection.scanFile(
           getApplicationContext(), 
           new String[]{dir.getAbsolutePath()}, 
           null, 
           new MediaScannerConnection.OnScanCompletedListener() { 
            @Override 
            public void onScanCompleted(String path, Uri uri) { 
             Log.v("LOG", "file " + path + " was scanned successfully: " + uri); 
            } 
           }); 
        } catch (Exception e) { 
         System.out.print(e.toString()); 
        } 
        //dismissing the dialog 
        dialog.cancel(); 
        dialog.dismiss(); 
       } 
      }); 

      // Get the AlertDialog from builder create() 
      AlertDialog dialog = builder.create(); 
      //Set dialog view/layout 
      dialog.setView(input); 

      //Add positive button to dialog 
      //When pressing the positive button a file with the specified name and configurtion is stored on internal storage 
      dialog.setButton(DialogInterface.BUTTON_POSITIVE, "Save", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        try { 
         filename = input.getText().toString(); 
         //Create text file in directory /Pump Config Files 
         File file = new File(dir.getAbsolutePath(), filename + ".txt"); 
         //create bufferedwrite with filewriter 
         BufferedWriter bw = new BufferedWriter(new FileWriter(file)); 
         //write to file --> an existing file will be overwritten 
         bw.write(datastring); 
         //Flush bufferedwriter 
         bw.close(); 
         //LOG 
         System.out.println("Wrote to file"); 
         //Show a message 
         Toast.makeText(getApplicationContext(), "Saved data", Toast.LENGTH_SHORT).show(); 
        } catch (Exception e) { 
         System.out.print(e.toString()); 
        } 
        // initiate media scan -->cause sometimes created files are nt shown on computer when connected to phone via USB 
        // restarting the smartphone or rename folder can help too 
        // make the scanner aware of the location and the files you want to see 
        MediaScannerConnection.scanFile(
          getApplicationContext(), 
          new String[]{dir.getAbsolutePath()}, 
          null, 
          new MediaScannerConnection.OnScanCompletedListener() { 
           @Override 
           public void onScanCompleted(String path, Uri uri) { 
            Log.v("LOG", "file " + path + " was scanned successfully: " + uri); 
           } 
          }); 

        dialog.cancel(); 
        dialog.dismiss(); 
       } 
      }); 

      //Add negative button 
      dialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        dialog.cancel(); 
       } 
      }); 
      //show the save dialog 
      dialog.show(); 
     } catch (Exception e) { 
      System.out.print(e.toString()); 
     } 
    } 

のAndroid API 23およびAPI 21 作品21の罰金ではなく、23

+0

'Log.d(MainActivity.LOG_TAG、" savebutton:ディレクトリが作成されていません ");'。あなたはその文の後ろにreturn文を忘れました。 – greenapps

+0

'new String [] {dir.getAbsolutePath()}、'。代わりにファイルをスキャンする必要があります: 'new String [] {file.getAbsolutePath()}、'。 – greenapps

答えて

0

ありがとうgreenapps上でテスト、

新しいString [] { dir.getAbsolutePath()} ,.新しい文字列[] {file.getAbsolutePath()}、

が問題なく動作します。

私はいつもこのような小さな細部が欠けています。

関連する問題