Files cannot be selected after calling ACTION_GET_CONTENT Intent

I am developing an application, and in this exercise a button is clicked and any file can be selected for download. File selection loads correctly, but all images are not selectable (grayed out). I added READ_EXTERNAL_STORAGE permission to the manifest file, but I have no idea why it still will not let me select the file. Here is the code I'm using

private Button uploadButton; private TextView uploadFile; private static final int PICKFILE_RESULT_CODE = 1; private String selectedImagePath; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); uploadButton = (Button)findViewById(R.id.upload_button); uploadFile = (TextView)findViewById(R.id.uploadFile); uploadButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v){ Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("file/*"); startActivityForResult(intent, PICKFILE_RESULT_CODE); }}); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { switch(requestCode){ case PICKFILE_RESULT_CODE: if (resultCode==RESULT_OK){ String FilePath = data.getData().getPath(); uploadFile.setText(FilePath); } break; } } 
+6
source share
3 answers

Blundell mentioned this in a comment above, but you can also add below to your manifest file. Also, try using setType ("image / *).

  <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" /> 
+4
source

Try setting type "*/* as suggested by Blundell .

If you do not want the user to be able to select content of any type, you need to register the file type with onActivityResult ( this answer shows how). Then try a couple of valid files, view the log and change intent.setType accordingly. You can use several types in KitKat and above, as described in this answer .

+8
source

When I tried with marshmellow and above, the code below helped. I tried with C # (xamarin). The key is the multiple mime type. Hope this helps someone.

 Intent intent = new Intent(Intent.ActionOpenDocument); intent.SetType("file/*"); intent.AddCategory(Intent.CategoryOpenable); String[] mimeTypes = { "text/csv", "text/comma-separated-values" ,"application/pdf","image/*"}; intent.PutExtra(Intent.ExtraMimeTypes, mimeTypes); ((FormsAppCompatActivity)Forms.Context).StartActivityForResult(intent, 7007); 
+2
source

Source: https://habr.com/ru/post/980377/


All Articles