Android Arsenal – Dateisystem
Sie können diese leichtgewichtige Bibliothek verwenden, um die Anhangsfunktion zu implementieren (Bilder mit der Kamera aufnehmen, Dateien / Bilder aus einer Galerie oder einem Dateisystem oder einer Google-Festplatte herunterladen). Die Bibliothek hilft Ihnen, alle Dateiauswahlprozesse zu vereinfachen, ohne sich Gedanken über Systemberechtigungen machen zu müssen
Sprachunterstützung
Warnung!
-
Diese Bibliothek wurde mit erstellt AndroidX.Also empfehle ich Ihnen, Ihr Projekt zu migrieren AndroidX Andernfalls kann es zu Problemen bei der gemeinsamen Verwendung von Androidx- und Libo-Unterstützung kommen.
-
Sie können einem Fehler begegnen
Invoke-customs are only supported starting with android 0 --min-api 26
.Um dies zu lösen, fügen Sie unterhalb der Zeile auf Anwendungsebene hinzu build.gradle Datei.
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
- Fügen Sie Berechtigungen und Anbieter hinzu AndroidManifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.attachmentmanager"
android:exported="false"
android:grantUriPermissions="true"
tools:replace="android:authorities">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider"
tools:replace="android:resource" />
</provider>
- Erstellen file_provider.xml in res/xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="myApp"
path="Download/" />
<external-files-path
name="images"
path="Pictures" />
</paths>
- Wenn Sie auf Android 11+ abzielen, müssen Sie die folgenden Abfragen hinzufügen AndroidManifest.xml
<queries>
<intent>
<action android:name="android.intent.action.OPEN_DOCUMENT" />
<!-- If you don't know the MIME type in advance, set "mimeType" to "*/*". -->
<data android:mimeType="*/*" />
</intent>
<intent>
<action android:name="android.intent.action.PICK" />
<!-- If you don't know the MIME type in advance, set "mimeType" to "*/*". -->
<data android:mimeType="*/*" />
</intent>
</queries>
- Aktualisieren Sie die Projektebene build.gradle Datei.
allprojects {
repositories {
jcenter()
maven { url "https://jitpack.io" } //Make sure to add this in your project
}
}
implementation 'com.github.Zaid-Mirza:AttachmentManager:2.0.1'
- Führen Sie das AttachmentManager-Objekt mithilfe des Erstellungsformulars aus
Kotlin
private var attachmentManager: AttachmentManager? = null
var gallery = arrayOf("image/png",
"image/jpg",
"image/jpeg")
var files = arrayOf("application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .ppt & .pptx
"application/pdf")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
attachmentManager = AttachmentManager.AttachmentBuilder(this) // must pass Context
.fragment(null) // pass fragment reference if you are in fragment
.setUiTitle("Choose File") // title of dialog or bottom sheet
.allowMultiple(false) // set true if you want make multiple selection, default is false
.asBottomSheet(true) // set true if you need to show selection as bottom sheet, default is as Dialog
.setOptionsTextColor(android.R.color.holo_green_light) // change text color
.setImagesColor(R.color.colorAccent) // change icon color
.hide(HideOption.DOCUMENT) // You can hide any option do you want
.setMaxPhotoSize(200000) // Set max photo size in bytes
.galleryMimeTypes(gallery) // mime types for gallery
.filesMimeTypes(files) // mime types for files
.build(); // Hide any of the three options
}
Java
private AttachmentManager attachmentManager = null;
String[] gallery = {"image/png",
"image/jpg",
"image/jpeg"};
String[] files = { "application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .ppt & .pptx
"application/pdf"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
attachmentManager = new AttachmentManager.AttachmentBuilder(this) // must pass Context
.fragment(null) // pass fragment reference if you are in fragment
.setUiTitle("Choose File") // title of dialog or bottom sheet
.allowMultiple(false) // set true if you want make multiple selection, default is false
.asBottomSheet(true) // set true if you need to show selection as bottom sheet, default is as Dialog
.setOptionsTextColor(android.R.color.holo_green_light) // change text color
.setImagesColor(R.color.colorAccent) // change icon color
.hide(HideOption.DOCUMENT) // You can hide any option do you want
.setMaxPhotoSize(200000) // Set max photo size in bytes
.galleryMimeTypes(gallery) // mime types for gallery
.filesMimeTypes(files) // mime types for files
.build(); // Hide any of the three options
}
- Deklarieren Sie registerForActivityResult
Kotlin
private var mLauncher = registerForActivityResult(StartActivityForResult()) { result ->
val list = attachmentManager?.manipulateAttachments(this,result.resultCode,result.data)
Toast.makeText(this, list?.size.toString(), Toast.LENGTH_LONG).show()
}
Java
ActivityResultLauncher<Intent> mLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
ArrayList<AttachmentDetail> list = attachmentManager.manipulateAttachments(this,result.getResultCode(),result.getData());
});
- Anruf openSelection () Methode zum Anzeigen der Benutzeroberfläche zum Auswählen und Weiterleiten von ActivityResultLauncher
Kotlin
attachmentManager?.openSelection(mLauncher)
Java
attachmentManager.openSelection(mLauncher);
- OnRequestPermissionsResult rückgängig machen (optional)
Kotlin
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
attachmentManager?.handlePermissionResponse(requestCode, permissions, grantResults)
}
Java
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
attachmentManager.handlePermissionResponse(requestCode,permissions,grantResults);
}
Andere Verwendungen
- Sie können eine Galerie, Kamera oder ein Dateisystem direkt öffnen, ohne dem Benutzer die Benutzerauswahlschnittstelle anzuzeigen
Kotlin
attachmentManager?.startCamera(mLauncher)
// OR
attachmentManager?.openGallery(mLauncher)
// OR
attachmentManager?.openFilSystem(mLauncher)
Java
attachmentManager.startCamera(mLauncher);
// OR
attachmentManager.openGallery(mLauncher);
// OR
attachmentManager.openFilSystem(mLauncher);
Notiz
Jede Art von Verbesserungen und Vorschlägen sind willkommen. Wenn Sie diese Bibliothek in Ihrem Projekt verwenden, teilen Sie mir bitte auch die URL Ihrer Anwendung mit. Ich werde Ihre Anwendung hier auflisten.