reading files from directory

Danchik

New member
Nov 10, 2024
1
0
1
Visit site
Hello everyone! Recently I encountered a problem I needed to make an application that should read files from a device, for example, photos, videos, music, and the like. When I wanted to open the downloads or images or music or other folder, my program could not read them.

please help me solve this problem

MainActivity.java
public class MainActivity extends AppCompatActivity {
//private TextView t;
private Button bt;
private File File;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;

});

bt = findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// val downloadFolder = requireContext().getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS
System.out.println("btn_click!");
// String path = Environment.getExternalStorageDirectory().toString();

// System.out.println(path);
// if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
// }

File f = new File("sdcard");
String[] s = f.list();
File sdcard = Environment.getExternalStorageDirectory();

//Get the text file

File file = new File(String.valueOf(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)));
String[] text = file.list();
//Read text from file

System.out.println(Arrays.toString(text));
}
// for(int i=0;i < f.length;i++){
// System.out.println(s[i]);
// }

});
}

}

manifect.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<!-- <permission android:name="android.permission.ACCESS_SUPERUSER"-->
<!-- tools:ignore="ReservedSystemPermission" />-->



<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
android:maxSdkVersion="28"
/>

<application
android:requestLegacyExternalStorage="true"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.New_proj"

android:protectionLevel="normal"

>
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
 

B. Diddy

Senior Ambassador
Moderator
Mar 9, 2012
167,232
7,432
113
Visit site
Welcome to Android Central! I moved this to the Software Development forum for more specific traffic.
 

Forum statistics

Threads
949,654
Messages
6,943,837
Members
3,161,577
Latest member
danlockauthor