How to Read Pen Drives connected through OTG?

为君一笑 提交于 2019-12-12 20:19:37

问题


Here is my code of my activity.

public class MainActivity extends AppCompatActivity {
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    lv=findViewById(R.id.chckExternal);

    filter();
}

this is my code of filter files...

//--------------------Filter files----------------
 public  ArrayList<File> findSongs(File root) {
    ArrayList<File> al = new ArrayList<>();
    for(File singleFile : root.listFiles())
    {
        if(singleFile.isDirectory()   && !singleFile.isHidden() ) {
            al.addAll(findSongs(singleFile));
        } else if(singleFile.getName().endsWith(".mp3") || 
singleFile.getName().endsWith(".MP3")) {
            al.add(singleFile);
        }
    }
    return al;
}

This is the method to set the filter values in listview.

public void filter(){
    ArrayList<File> 
arrayList=findSongs(Environment.getExternalStorageDirectory());
    ArrayList<String>  convertedItemsList=new 
ArrayList(arrayList.size());
    for(int j=0;j < arrayList.size(); j++) {
      convertedItemsList.add(arrayList.get(j)
 .getParentFile().getName().toString());
    }

    ArrayAdapter arrayAdapter=new 
  ArrayAdapter(this,android.R.layout.simple_list_item_1,
  convertedItemsList);
    lv.setAdapter(arrayAdapter);
}
}

This code is able to read the files from internal storage of device but unable to read from the external storage like OTG and removable Sd card....


回答1:


This method will help you to get the path of removable sdcards but unable to give you the path of pen drive connected through OTG....

  //--------------- Method to retrieve connected storage--------------------------//

private static final Pattern DIR_SEPORATOR = Pattern.compile("/");

public static String[] getStorageDirectories()
{
    // Final set of paths
    final Set<String> rv = new HashSet<String>();
    // Primary physical SD-CARD (not emulated)
    final String rawExternalStorage = System.getenv("EXTERNAL_STORAGE");
    // All Secondary SD-CARDs (all exclude primary) separated by ":"
    final String rawSecondaryStoragesStr = System.getenv("SECONDARY_STORAGE");
    // Primary emulated SD-CARD
    final String rawEmulatedStorageTarget = System.getenv("EMULATED_STORAGE_TARGET");
    if(TextUtils.isEmpty(rawEmulatedStorageTarget))
    {
        // Device has physical external storage; use plain paths.
        if(TextUtils.isEmpty(rawExternalStorage))
        {
            // EXTERNAL_STORAGE undefined; falling back to default.
            rv.add("/storage/sdcard0");
        }
        else
        {
            rv.add(rawExternalStorage);
        }
    }
    else
    {
        // Device has emulated storage; external storage paths should have
        // userId burned into them.
        final String rawUserId;
        if(Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
        {
            rawUserId = "";
        }
        else
        {
            final String path = 
   Environment.getExternalStorageDirectory().getAbsolutePath();
            final String[] folders = DIR_SEPORATOR.split(path);
            final String lastFolder = folders[folders.length - 1];
            boolean isDigit = false;
            try
            {
                Integer.valueOf(lastFolder);
                isDigit = true;
            }
            catch(NumberFormatException ignored)
            {
            }
            rawUserId = isDigit ? lastFolder : "";
        }
        // /storage/emulated/0[1,2,...]
        if(TextUtils.isEmpty(rawUserId))
        {
            rv.add(rawEmulatedStorageTarget);
        }
        else
        {
            rv.add(rawEmulatedStorageTarget + File.separator + rawUserId);
        }
    }
    // Add all secondary storages
    if(!TextUtils.isEmpty(rawSecondaryStoragesStr))
    {
        // All Secondary SD-CARDs splited into array
        final String[] rawSecondaryStorages = 
     rawSecondaryStoragesStr.split(File.pathSeparator);
        Collections.addAll(rv, rawSecondaryStorages);
    }
    return rv.toArray(new String[rv.size()]);
}

It gives you array of storages....Try this already tested on lenovo, huawei, honor,vivo



来源:https://stackoverflow.com/questions/54022262/how-to-read-pen-drives-connected-through-otg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!