Ich habe versucht, „Lesen, Bearbeiten, Verwalten des Speichers in Android Java“ zu erhalten, und die AusgabeberechtigungJava

Java-Forum
Guest
 Ich habe versucht, „Lesen, Bearbeiten, Verwalten des Speichers in Android Java“ zu erhalten, und die Ausgabeberechtigung

Post by Guest »

Die von mir entwickelte App kann alle Dokumente wie PDF und Office PowerPoint, Excel, Word lesen.
Und die Hauptaktivität besteht darin, eine Liste aller auf dem Gerät verfügbaren Dokumente anzuzeigen, damit der Benutzer jedes Dokument in der App öffnen kann und nicht Nur Dateien aus dem Speicher durchsuchen
Ich habe diesen Code ausprobiert, aber er funktionierte nicht und habe mehrere andere Lösungen in Stackoverflow und anderen Websites ausprobiert und am Ende hat nichts funktioniert.
mein Code
Hauptaktivität
vor dem Erstellen

Code: Select all

private static final int PDF_SELECTION_CODE = 200;
private static final int PERMISSION_REQUEST_CODE = 100;
private RecyclerView recyclerView;
private DocumentAdapter documentAdapter;
private ArrayList documentList;
inside on create

Code: Select all

recyclerView = findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_CODE);
loadDocuments();
} else {
loadDocuments();
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
// Scoped Storage
loadDocumentsWithScopedStorage();
} else {
// Legacy Storage
loadDocumentsWithScopedStorage();
}

loadDocumentsWithMediaStore();

nach dem Erstellen

Code: Select all

private void getDocuments(@NonNull File dir) {
File[] files = dir.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
getDocuments(file);
} else if (file.getName().endsWith(".pdf") ||
file.getName().endsWith(".doc") ||
file.getName().endsWith(".docx") ||
file.getName().endsWith(".txt")) {
documentList.add(file);
}
}
}
}

private void loadDocumentsWithScopedStorage() {
documentList = new ArrayList();
Uri collection = MediaStore.Files.getContentUri("external");

String[] projection = {
MediaStore.Files.FileColumns.DATA,
MediaStore.Files.FileColumns.DISPLAY_NAME
};

// Filter for specific document types
String selection = MediaStore.Files.FileColumns.MIME_TYPE + " IN (?, ?, ?, ?)";
String[] selectionArgs = new String[]{
"application/pdf",
"application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"text/plain"
};

Cursor cursor = getContentResolver().query(collection, projection, selection, selectionArgs, null);

if (cursor != null) {
int dataIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);

while (cursor.moveToNext()) {
String filePath = cursor.getString(dataIndex);
File file = new File(filePath);
documentList.add(file);
}
cursor.close();
}

documentAdapter = new DocumentAdapter(documentList, file -> {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(android.net.Uri.fromFile(file), "*/*");
startActivity(intent);
});
recyclerView.setAdapter(documentAdapter);
}

private void loadDocumentsWithMediaStore() {
documentList = new ArrayList();
Uri collection = MediaStore.Files.getContentUri("external");

String[] projection = {
MediaStore.Files.FileColumns.DATA,
MediaStore.Files.FileColumns.DISPLAY_NAME
};

String selection = MediaStore.Files.FileColumns.MIME_TYPE + "  IN (?, ?, ?, ?)";
String[] selectionArgs = new String[]{
"application/pdf",
"application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
"text/plain"
};

try (Cursor cursor = getContentResolver().query(collection, projection, selection, selectionArgs, null)) {
if (cursor != null) {
int dataIndex = cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);

while (cursor.moveToNext()) {
String filePath = cursor.getString(dataIndex);
File file = new File(filePath);
documentList.add(file);
}
}
}

documentAdapter = new DocumentAdapter(documentList, file -> {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(android.net.Uri.fromFile(file), "*/*");
startActivity(intent);
});
recyclerView.setAdapter(documentAdapter);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
loadDocuments();
} else {
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
}
}
}

Dokumentenadapter

Code: Select all

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.io.File;
import java.util.ArrayList;

public class DocumentAdapter extends RecyclerView.Adapter {

private final ArrayList documentList;
private final OnDocumentClickListener listener;

public DocumentAdapter(ArrayList documentList, OnDocumentClickListener listener) {
this.documentList = documentList;
this.listener = listener;
}

@NonNull
@Override
public DocumentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
return new DocumentViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull DocumentViewHolder holder, int position) {
File document = documentList.get(position);
holder.textView.setText(document.getName());
holder.itemView.setOnClickListener(v -> listener.onDocumentClick(document));
}

@Override
public int getItemCount() {
return documentList.size();
}

public interface OnDocumentClickListener {
void onDocumentClick(File file);
}

static class DocumentViewHolder extends RecyclerView.ViewHolder {
TextView textView;

public DocumentViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(android.R.id.text1);
}
}
}
Ich habe alle Lösungen ausprobiert, vom Durchsuchen von Stackoverflow-Fragen über die Suche bei Google bis hin zum Fragen von Gemini und Chatgpt.
Code funktioniert nicht und zeigt keine Liste der Dokumentdateien an
Es wird kein Codefehler angezeigt und wenn ich die App ausführe, wird nur die Toastmeldung „Berechtigung verweigert“ ausgegeben.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post