Compare commits
No commits in common. "f8a38d93664209abbc55fb0034dfb6dfd0cf9712" and "787f8439811bf152303a22512cd949d54c90f8c2" have entirely different histories.
f8a38d9366
...
787f843981
|
|
@ -45,13 +45,6 @@ public class CreateDocumentGuide implements ActivityResultCallback<Uri> {
|
|||
.registerForActivityResult(createDocumentContract, this);
|
||||
}
|
||||
|
||||
public static CreateDocumentGuide anyMimeType(
|
||||
Context context,
|
||||
ActivityResultCaller activityResultCaller
|
||||
){
|
||||
return new CreateDocumentGuide(context, activityResultCaller, "*/*");
|
||||
}
|
||||
|
||||
@Override
|
||||
public final void onActivityResult(Uri o) {
|
||||
uriHandler.handleUri(o);
|
||||
|
|
|
|||
|
|
@ -1,7 +0,0 @@
|
|||
package com.proculite.androidagain.common.storageaccess;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
public interface InputStreamHandler {
|
||||
void handleInputStream(InputStream inputStream);
|
||||
}
|
||||
|
|
@ -1,62 +0,0 @@
|
|||
package com.proculite.androidagain.common.storageaccess;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.activity.result.ActivityResultCallback;
|
||||
import androidx.activity.result.ActivityResultCaller;
|
||||
import androidx.activity.result.ActivityResultLauncher;
|
||||
import androidx.activity.result.contract.ActivityResultContracts;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
public class OpenDocumentGuide implements ActivityResultCallback<Uri> {
|
||||
private final String TAG = ActivityResultContracts.OpenDocument.class.getName();
|
||||
private final ActivityResultLauncher<String[]> openDocumentLauncher;
|
||||
private final Context context;
|
||||
private UriHandler uriHandler;
|
||||
|
||||
public OpenDocumentGuide(
|
||||
Context context,
|
||||
ActivityResultCaller activityResultCaller
|
||||
) {
|
||||
this.context = context;
|
||||
ActivityResultContracts.OpenDocument openDocumentContract =
|
||||
new ActivityResultContracts.OpenDocument();
|
||||
openDocumentLauncher = activityResultCaller.registerForActivityResult(openDocumentContract, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onActivityResult(Uri o) {
|
||||
uriHandler.handleUri(o);
|
||||
}
|
||||
|
||||
public void readFromInputStream(String[] suggestedMimeTypes, InputStreamHandler inputStreamHandler){
|
||||
uriHandler = uri -> {
|
||||
try {
|
||||
InputStream inputStream = context.getContentResolver().openInputStream(uri);
|
||||
if(inputStream == null)
|
||||
{
|
||||
Log.e(TAG, "Obtained input stream is null.");
|
||||
return;
|
||||
}
|
||||
inputStreamHandler.handleInputStream(inputStream);
|
||||
inputStream.close();
|
||||
}catch (FileNotFoundException e){
|
||||
Log.e(TAG, "FileNotFoundException occurred on read from InputStream.");
|
||||
} catch (IOException e) {
|
||||
Log.e(TAG, "IOException occurred on read from InputStream.");
|
||||
}
|
||||
};
|
||||
|
||||
openDocumentLauncher.launch(suggestedMimeTypes);
|
||||
}
|
||||
|
||||
public void readFromInputStream(InputStreamHandler inputStreamHandler){
|
||||
// Any MIME type.
|
||||
readFromInputStream(new String[]{"*/*"},inputStreamHandler);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue