Android : 인 텐트를 통해 특정 폴더를 열고 파일 브라우저에 해당 콘텐츠를 표시하는 방법은 무엇입니까?
나는 이것이 쉬울 것이라고 생각했지만 불행히도 그렇지 않습니다.
내가 가진 것 :
외부 저장소에 "myFolder"라는 폴더가 있습니다 (Nexus 4이기 때문에 sd 카드가 아니지만 문제가되지 않습니다). 폴더에는 일부 *.csv파일이 포함되어 있습니다.
내가 원하는 것 :
다음을 수행하는 방법을 작성하고 싶습니다. 하나를 선택할 수있는 다양한 앱 (파일 브라우저)을 표시합니다 (그림 참조). 클릭하면 선택한 파일 브라우저가 시작되고 "myFolder"의 내용이 표시됩니다. 그 이상도 이하도 아닌.

내 질문:
정확히 어떻게해야합니까? 나는 다음 코드와 매우 비슷하다고 생각하지만 내가 무엇을하든 내가 아직 옳지 않은 것이 분명하다고 확신합니다. 항상 외부 저장소의 기본 폴더 만 열립니다.
public void openFolder()
{
File file = new File(Environment.getExternalStorageDirectory(),
"myFolder");
Log.d("path", file.toString());
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setDataAndType(Uri.fromFile(file), "*/*");
startActivity(intent);
}
이것은 작동합니다.
Uri selectedUri = Uri.parse(Environment.getExternalStorageDirectory() + "/myFolder/");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(selectedUri, "resource/folder");
if (intent.resolveActivityInfo(getPackageManager(), 0) != null)
{
startActivity(intent);
}
else
{
// if you reach this place, it means there is no any file
// explorer app installed on your device
}
장치에 파일 탐색기 앱이 설치되어 있는지 확인하십시오.
편집 : 댓글에서 shantanu의 추천을 추가했습니다.
라이브러리 : 현재 솔루션이 도움이되지 않는 경우 https://android-arsenal.com/tag/35 파일 / 디렉터리 선택기 라이브러리를 살펴볼 수도 있습니다.
마침내 작동하게되었습니다. 이렇게하면 선택기 (Google Drive, Dropbox, Root Explorer 및 Solid Explorer)에 몇 개의 앱만 표시됩니다. 두 명의 탐험가에서는 잘 작동하지만 Google Drive 및 Dropbox에서는 작동하지 않습니다 (외부 저장소에 액세스 할 수 없기 때문에 추측합니다). 다른 MIME 유형 "*/*"도 가능합니다.
public void openFolder(){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+ File.separator + "myFolder" + File.separator);
intent.setDataAndType(uri, "text/csv");
startActivity(Intent.createChooser(intent, "Open folder"));
}
Intent chooser = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getDownloadCacheDirectory().getPath().toString());
chooser.addCategory(Intent.CATEGORY_OPENABLE);
chooser.setDataAndType(uri, "*/*");
// startActivity(chooser);
try {
startActivityForResult(chooser, SELECT_FILE);
}
catch (android.content.ActivityNotFoundException ex)
{
Toast.makeText(this, "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
위 코드에서 setDataAndType이 "* / *"이면 파일을 선택할 수있는 내장 파일 브라우저가 열리고 "text / plain"을 설정하면 Dropbox가 열립니다. Dropbox, Google Drive가 설치되어 있습니다. Dropbox를 제거하면 "* / *"만 작동하여 파일 브라우저를 엽니 다. 이것은 Android 4.4.2입니다. Dropbox와 Google Drive에서 getContentResolver (). openInputStream (data.getData ())를 통해 콘텐츠를 다운로드 할 수 있습니다.
이 코드는 OI 파일 관리자에서 작동합니다.
File root = new File(Environment.getExternalStorageDirectory().getPath()
+ "/myFolder/");
Uri uri = Uri.fromFile(root);
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setData(uri);
startActivityForResult(intent, 1);
you can get OI File manager here : http://www.openintents.org/en/filemanager
Today, you should be representing a folder using its content: URI as obtained from the Storage Access Framework, and opening it should be as simple as:
Intent i = new Intent(Intent.ACTION_VIEW, uri);
startActivity(i);
Alas, the Files app currently contains a bug that causes it to crash when you try this using the external storage provider. Folders from third party providers however can be displayed in this way.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("text/csv");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"), 0);
} catch (android.content.ActivityNotFoundException ex) {
ex.printStackTrace();
}
then you just need to add the response
public void onActivityResult(int requestCode, int resultCode, Intent data){
switch (requestCode) {
case 0: {
//what you want to do
//file = new File(uri.getPath());
}
}
}
File temp = File.createTempFile("preview", ".png" );
String fullfileName= temp.getAbsolutePath();
final String fileName = Uri.parse(fullfileName)
.getLastPathSegment();
final String filePath = fullfileName.
substring(0,fullfileName.lastIndexOf(File.separator));
Log.d("filePath", "filePath: " + filePath);
fullfileName:
/mnt/sdcard/Download_Manager_Farsi/preview.png
filePath:
/mnt/sdcard/Download_Manager_Farsi
You seem close.
I would try to set the URI like this :
String folderPath = Environment.getExternalStorageDirectory()+"/pathTo/folder";
Intent intent = new Intent();
intent.setAction(Intent.ACTION_GET_CONTENT);
Uri myUri = Uri.parse(folderPath);
intent.setDataAndType(myUri , "file/*");
startActivity(intent);
But it's not so different from what you have tried. Tell us if it changes anything ?
또한 대상 폴더가 있는지 확인하고 의도 로 보내기 전에 결과 Uri 개체를 살펴보십시오 . 예상 한 것과 다를 수 있습니다.
'program story' 카테고리의 다른 글
| 키가 존재하지 않는 맵의 값을 읽으면 어떻게됩니까? (0) | 2020.11.28 |
|---|---|
| CSS를 사용하여 웹 양식의 특정 컨트롤 주위에 그룹 상자 만들기 (0) | 2020.11.28 |
| Amazon RDS Oracle 인스턴스에 연결할 때 "읽기 호출에서 빼기 1"오류에 접근하는 방법 (0) | 2020.11.28 |
| gateway.sandbox.push.apple.com에 연결할 때 "verify error : num = 20" (0) | 2020.11.28 |
| null 객체 참조에 대한 android.content.Context.getPackageName () ' (0) | 2020.11.28 |