Handle Download Intents In My App
My webview app already handles external URL's fine (opens links from external apps like viber, browsers, etc) like this- (i got this code from here) // get URL from browsable inten
Solution 1:
you have to solve issues to
- (a) always show the chooser
- (b) tell android that your app should become part of the chooser
- (c) process the "download"
(a) always show the chooser
replace
startActivity(i);
with
startActivity(Intent.createChooser(i, "caption of the choser"));
(b) tell android that your app should become part of the chooser
In the manifest you have to declare that you have an activity that can handle the relevant content
<manifest... ><application... ><activityandroid:name=".MyDownloadActivity"...><intent-filter ><actionandroid:name="android.intent.action.VIEW" /><actionandroid:name="android.intent.action.SEND" /><actionandroid:name="android.intent.action.SENDTO" /><categoryandroid:name="android.intent.category.DEFAULT" /><categoryandroid:name="android.intent.category.BROWSABLE" /><dataandroid:mimeType="*/*"android:scheme="file" /></intent-filter><intent-filter ><!-- mime only SEND(_TO) --><actionandroid:name="android.intent.action.SEND" /><actionandroid:name="android.intent.action.SENDTO" /><actionandroid:name="android.intent.action.VIEW" /><categoryandroid:name="android.intent.category.DEFAULT" /><categoryandroid:name="android.intent.category.BROWSABLE" /><categoryandroid:name="android.intent.category.CATEGORY_OPENABLE" /><!--
these do not work :-(
<data android:scheme="content" android:host="*" />
<data android:scheme="content"
android:host="media"
android:pathPattern="/external/images/media/.*" />
<data android:host="*" android:scheme="content"
android:mimeType="image/*" />
--><dataandroid:mimeType="*/*" /></intent-filter></activity></application></manifest>
* (c) process the "download"
MyDownloadActivity
Your MyDownloadActivity.onCreate()
will receive all mime-types * / * for SEND, SENDTO, VIEW. For Details see @Vickyexpert-s answer
intent-intercept is a free android tool to analyse intents. it can intercept nearly every intent. you can look at the sourcecode to see how this is done.
I use sendtosd as my general purpose download handler. It-s sourcecoder is available on github
Solution 2:
You need to put below code in your activity which response the intent,
voidonCreate(Bundle savedInstanceState) {
// Get intent, action and MIME typeIntent intent = getIntent();
String action = intent.getAction();
Stringtype = intent.getType();
if (Intent.ACTION_VIEW.equals(action))
{
*/if (type.startsWith("image/“))
{*/
downloadImage(intent); // Download image being sent
}
}
voiddownloadImage(Intent intent) {
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (imageUri != null) {
// Write Your Download Code Here
}
}
Post a Comment for "Handle Download Intents In My App"