Vaibhav Agarwal Vaibhav Agarwal - 2 months ago 27
Android Question

Pick file from Custom gallery of Audio and Video files

I have created a

Gallery Activity
which contains list of audio and video files located on SD Card. I have another activity through which I want to pick files from
Gallery Activity
using
Intent
. I have added following
intent-filter
to manifest:

<activity
android:name=".Activities.GalleryActivity"
android:icon="@drawable/gallery"
android:label="@string/gallery"
android:parentActivityName=".Activities.MainActivity">

<intent-filter >
<action android:name="android.intent.action.PICK" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="file" />
</intent-filter>
<intent-filter >
<action android:name="android.intent.action.GET_CONTENT" />
<category android:name="android.intent.category.OPENABLE" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="audio/*" />
<data android:mimeType="video/*" />
</intent-filter>
</activity>


Here is how I have created
Intent
to pick file:

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
startActivityForResult(intent, ATTACH_FILES);


EDITS:
Here is item
onClickListener
of
Gallery Activity
:


viewAdapter.withOnClickListener(new FastAdapter.OnClickListener<GalleryRowContent>() {
@Override
public boolean onClick(View v, IAdapter<GalleryRowContent> adapter, GalleryRowContent item, int position) {

if (adapter.getFastAdapter().getSelections().size() == 0) {

if (item.getTag().equals("audio")) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(item.getFilePath());
intent.setDataAndType(Uri.fromFile(file), "audio/*");
startActivity(intent);

} else if (item.getTag().equals("video")) {
Intent intent = new Intent();
intent.setAction(android.content.Intent.ACTION_VIEW);
File file = new File(item.getFilePath());
intent.setDataAndType(Uri.fromFile(file), "video/*");
startActivity(intent);
}
}

return false;
}
});


This code shows my app in intent chooser and also opens
Gallery Activity
, however when I click on any file, it opens the file intead of picking it. Can anyone help me?

Answer

To return a result from an activity started by startActivityForResult(), call setResult(), supplying the Intent containing the "result". Usually, this is immediately followed by a call to finish(), so control returns to the activity that had called startActivityForResult(), so it can use the result.