2017-08-09 48 views
0

我是Android开发新手。 我确定很多人都有同样的问题。我有一个银河S7手机没有SD卡。所以没有外部存储。但我想创建文件与我的应用程序必须从Windows资源管理器访问导出它。Android Studio - 访问/存储/模拟/ 0从Windows资源管理器和Android文件管理器

注:debbug在USB模式 - 无虚拟设备

当然,在我的清单文件我已经设置好的那些2个权限:

android.permission.WRITE_INTERNAL_STORAGE 
android.permission.WRITE_EXTERNAL_STORAGE 

可以肯定我已经创建了一个文件,我用这个写方法和下面的读取方法:

File root = this.getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS); 
    File customFolder = new File(root.getAbsolutePath() + "/CustomFolder"); 
    customFolder.mkdirs(); 
    file = new File(customFolder, "myData.txt"); 

    // Must catch FileNotFoundException and IOException 
    try { 
     FileOutputStream f = new FileOutputStream(file); 
     PrintWriter pw = new PrintWriter(f); 
     pw.println("Howdy do to you,"); 
     pw.println("and the horse you rode in on."); 
     pw.flush(); 
     pw.close(); 
     f.close(); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
     Log.i(TAG, "File not found"); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     Log.i(TAG, "I/O exception"); 
    } 

要读取myData.txt

try { 
BufferedReader br = new BufferedReader(new FileReader(file)); 
     String line; 
     while (true) { 
      line= br.readLine(); 
      if (line== null) break; 
      tv.append("\n" + " " + line); 
     } 
     br.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

所以所有的方法都可以,但是当我浏览我的文件系统来获取创建的文件myData.txt时,我找不到它!

大多数我们安装在根自己的文件夹中的应用程序,像Snapchat,WhatsApp的的等等等等

我倒要做出同样的事情,什么是写在文件的方式:

ROOT - > MyApplicationName - > CustomFolder

感谢您的帮助:)

回答

0

我终于找到了一个解决方案添加MediaScannerConnection.scanFile()方法。

MediaScannerConnection.scanFile(getApplicationContext(),new String[]{file.getAbsolutePath()},null,new MediaScannerConnection.OnScanCompletedListener() { 
     @Override 
     public void onScanCompleted(String path, Uri uri) { 
      // Do something here if you want 
     }}); 

但这种解决方案并不完全适合我,因为存储文件夹:

的Android /数据/ donain_name /文件/文件/ CustomFolder

我想有同样的结果像whatsapp应用程序可以在内部存储根目录中有一个文件夹。

我想要的结果是: 为MyApplication/CustomeFolder

相关问题