Weird URI behavior java/setDataSource/MediaPlayer - java

I am using a URI that i pass to a MediaPlayer on android like this:
mediaPlayer.setDataSource(context, Uri.parse(<uri>));
When I get it using Intent.createChooser, it plays once then trying to make it play it again result in java.io.IOException: setDataSource failed.: status=0x80000000.
When i pass the URI as a string directly, it results in java.io.IOException: setDataSource failed.: status=0x80000000, despite the URI outputed by the selector being always the same.
The uri look like this: "content://com.android.providers.media.documents/document/audio%3A21739".
Can someone please enlighten on why does this happen?

Turns out android required some weird permissions shenanigans, the solutions was using a different opening coupled with using some sort of permission requirement that somehow output the same uri, but with a persistent access:
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, "Select file to add"), ADD_2);
In onActivityResult:
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
final int takeFlags = data.getFlags() & Intent.FLAG_GRANT_READ_URI_PERMISSION;
getContentResolver().takePersistableUriPermission(uri, takeFlags);
Log.d(TAG, "Added track uri: " + uri);
playlist.add(uri.toString());
adapter.notifyDataSetChanged();
}

Related

Android - Java - Intent to send facebook chat message (facebook disabled xmpp)

***Facebook deprecated xmpp api.
Is there a way to open an intent (or pass data to fb) to send chat message on android device?
Facebook & Messenger apps installed on the device.
Thanks :-)
You need to pass uri to the intent
Here 100005727832736 is the user id of the person who you want to
message to
Uri uri = Uri.parse("fb-messenger://user/100005727832736");
Here is my sample code
Uri uri = Uri.parse("fb-messenger://user/100005727832736");
Intent toMessenger= new Intent(Intent.ACTION_VIEW, uri);
try {
startActivity(toMessenger);
}
catch (android.content.ActivityNotFoundException ex)
{
Toast.makeText(context, "Please Install Facebook Messenger", Toast.LENGTH_LONG).show();
}
}
This is what worked for me and i haven not tested this for some time now.
To launch facebook app let urlString = "fb://page/your_fb_page_id"
To launch facebook messenger let urlString = "fb-messenger://user/your_fb_page_id"
FB page id is usually numeric. To get it, goto Find My FB ID input your profile url, something like www.facebook.com/edgedevstudio then click "Find Numberic ID".
Voila, you now have your fb numeric id. replace "your_fb_page_id" with the generated Numeric ID
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(urlString))
if (intent.resolveActivity(packageManager) != null) //check if app is available to handle the implicit intent
startActivity(intent)

Intent for sending Instagram direct message

I want people to be able to send a direct message to my Instagram account from my android application.
What is the intent for sending a direct message on Instagram?
Thanks
You can use below code
String type = "video/*";
String filename = "/myVideo.mp4";
String mediaPath = Environment.getExternalStorageDirectory() + filename;
createInstagramIntent(type, mediaPath);
private void createInstagramIntent(String type, String mediaPath){
// Create the new Intent using the 'Send' action.
Intent share = new Intent(Intent.ACTION_SEND);
// Set the MIME type
share.setType(type);
// Create the URI from the media
File media = new File(mediaPath);
Uri uri = Uri.fromFile(media);
// Add the URI to the Intent.
share.putExtra(Intent.EXTRA_STREAM, uri);
share.putExtra(Intent.EXTRA_STREAM, [URI of photo]);
share.putExtra(Intent.EXTRA_TEXT, [Text of caption]);
// Broadcast the Intent.
startActivity(Intent.createChooser(share, "Share to"));
}
See official documentation
See this & this also
To Share plain text to the Instagram DM(Direct) use following:
val shareIntent = Intent(Intent.ACTION_SEND)
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Assd") // set uri
shareIntent.setPackage("com.instagram.android")
startActivity(shareIntent)
Or You Also can use a file instead:
String type = "video/*";
...
intent..putExtra(Intent.EXTRA_STREAM, [URI of photo]);

How to send app invites from my application (via whatsapp,hike,message)

I am new for Android. I created a simple food app. I want to send invitation to my friends from my app via Whatsapp, message etc. while clicking the invite button. I don't have any idea about that. Can you anyone guide me (Show some examples means more helpful to me).
Thanks in advance!
Refer this link
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
private void onShareClicked() {
String link = "https://play.google.com/store/apps/details?id=com.recharge2mePlay.recharge2me";
Uri uri = Uri.parse(link);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, link.toString());
intent.putExtra(Intent.EXTRA_TITLE, "Recharge2me");
startActivity(Intent.createChooser(intent, "Share Link"));
}
And simply call this function in onClickListner.
btn_tellAFreind.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onShareClicked();
}
});
try this tutorial from google using Firebase, maybe this can help you:
https://codelabs.developers.google.com/codelabs/firebase-android/#10
You can generate short link by using the following codes:
DynamicLink dynamicLink = FirebaseDynamicLinks.getInstance().createDynamicLink()
.setLink(Uri.parse("https://play.google.com/store/apps/details?id=com.example"))
.setDynamicLinkDomain("abc.app.goo.gl")
.setAndroidParameters(new DynamicLink.AndroidParameters.Builder().build())
.buildDynamicLink();
Task<ShortDynamicLink> shortLinkTask = FirebaseDynamicLinks.getInstance().createDynamicLink()
.setLongLink(dynamicLink.getUri())
.buildShortDynamicLink()
.addOnCompleteListener(ReferrerActivity.this, new OnCompleteListener<ShortDynamicLink>() {
#Override
public void onComplete(#NonNull Task<ShortDynamicLink> task) {
if (task.isSuccessful()) {
Uri shortLink = task.getResult().getShortLink();
Uri flowchartLink = task.getResult().getPreviewLink();
Log.i(TAG, "onComplete: SHORTLINK " + shortLink.toString());
Log.i(TAG, "onComplete: FLOW LINK " + flowchartLink.toString());
} else {
Log.i(TAG, "onComplete: ERROR " + task.isSuccessful() + " " + task.isComplete());
}
}
});
Once you received the short link in onComplete method, share it using intent.
Firebase Invites are an out-of-the-box solution for app referrals and sharing via email or SMS.
Connect your app to your Firebase project from Firebase console
Enabled Firebase Dynamic Links from the Firebase console by opening the Dynamic Links section and accepting the terms of service if prompted.
Add Firebase to your Android project
Add the dependency for Firebase Invites to your app-level build.gradle file:
compile 'com.google.firebase:firebase-invites:10.0.1'
Send invitations:
Start by building an Intent using the AppInviteInvitation.IntentBuilder class:
Intent intent = new AppInviteInvitation.IntentBuilder(getString(R.string.invitation_title))
.setMessage(getString(R.string.invitation_message))
.setDeepLink(Uri.parse(getString(R.string.invitation_deep_link)))
.setCustomImage(Uri.parse(getString(R.string.invitation_custom_image)))
.setCallToActionText(getString(R.string.invitation_cta))
.build();
startActivityForResult(intent, REQUEST_INVITE);
Launching the AppInviteInvitation intent opens the contact chooser where the user selects the contacts to invite. Invites are sent via email or SMS. After the user chooses contacts and sends the invite, your app receives a callback to onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, "onActivityResult: requestCode=" + requestCode + ", resultCode=" + resultCode);
if (requestCode == REQUEST_INVITE) {
if (resultCode == RESULT_OK) {
// Get the invitation IDs of all sent messages
String[] ids = AppInviteInvitation.getInvitationIds(resultCode, data);
for (String id : ids) {
Log.d(TAG, "onActivityResult: sent invitation " + id);
}
} else {
// Sending failed or it was canceled, show failure message to the user
// ...
}
}
}
Check out here for more details about Send and Receive Firebase Invites from Your Android App
Update:
Use Branch sdk to support invite feature on another platform like WhatsApp, Facebook and other social media apps.
Check out here to know How branch link works?
Checkout here for Install guide & code example
If I understand the question correctly, you want to make the share differentiate when the user clicks share and if he selects to invite via WhatsApp, for example, it will show only Whatsapp as a medium option to share the invite through.
If that is what you want, you should to set the package in the intent you will use for sharing, so if we add to #Vishal answer above
sendIntent.setPackage("com.whatsapp");
You should do the same for any needed social media
You need to add Firebase Dynamic link. Earlier it was Firebase Invites but it is depreciated now. Firebase Dymanic Link is build over Firebase Invites So you can see the invites dependency on gradle file.
You can follow this tutorial for complete example about "How to Create Refer a friend Link"
There is Two ways to create "Refer a Friend Link"
Using Firebase base Dynamic Object
Manually Create Refer a Link
1) Option :-
DynamicLink dynamicLink = FirebaseDynamicLinks.getInstance().createDynamicLink()
.setLink(Uri.parse("https://www.example.com/"))
//.setDomainUriPrefix("https://example.page.link") // no longer in user please
.setDynamicLinkDomain("example.page.link") // use this code and don't use https:// here
// Open links with this app on Android
.setAndroidParameters(new DynamicLink.AndroidParameters.Builder().build())
// Open links with com.example.ios on iOS
.setIosParameters(new DynamicLink.IosParameters.Builder("com.example.ios").build())
.buildDynamicLink();
Uri dynamicLinkUri = dynamicLink.getUri();
2) Option:-
String sharelinktext = "https://referearnpro.page.link/?"+
"link=https://www.blueappsoftware.com/"+
"&apn="+ getPackageName()+
"&st="+"My Refer Link"+
"&sd="+"Reward Coins 20"+
"&si="+"https://www.blueappsoftware.com/logo-1.png";
Then Call ShortDynamicLink Object
Refer Link will be look like this :
https://referearnpro.page.link?apn=blueappsoftware.referearnpro&link=https%3A%2F%2Fwww.blueappsoftware.com%2F
You can check complete example here
a custom method to send invites, also according to the best practice in Android Developers
fun inviteToDownloadApp(context: Context) {
val intent = Intent(Intent.ACTION_SEND)
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_SUBJECT, context.getString(R.string.app_name))
intent.putExtra(Intent.EXTRA_TEXT, context.getString(R.string.invite_to_download_app))
context.startActivity(Intent.createChooser(intent, context.getString(R.string.invite_to_download_app)))
}

FATAL EXCEPTION: main java.lang.NullPointerException: uriString while trying to share image to social media intent Android

Hello I'm trying to enable my android app to post an image made from the app to facebook or twitter or instagram. I copied an exact example from someone else and it still doesn't work.
public void ShareToSocialMedia(String application) {
Bitmap b =BitmapFactory.decodeResource(getResources(),
R.mipmap.ic_launcher);
Intent share = new Intent(Intent.ACTION_SEND);
share.setPackage(application);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(getContentResolver(),
b, "Title", null);
Uri imageUri = Uri.parse(path);
share.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(share, "Select"));
}
I'm getting an error at the line
Uri imageUri = Uri.parse(path);
and I don't know why. can someone show me how to resolve this please?
If you look at the documentation for Uri.parse, you'll see that it throws a NullPointerException if the passed uriString is null.
Then looking at the MediaStore.Images.Media.insertImage method that you depend on to obtain the uriString, the documentation states that it returns null if the image failed to be stored for any reason.
Now, as to what that reason is, your guess is as good as mine, but a search on Stackoverflow does reveal that there are people experiencing similar issues.

Android how to open Deezer from URI

I want to open the Deezer Android Application from URI using Android Intent.
My code is:
try {
uri = "deezer://track/"+track_id;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
activity.startActivity(intent);
} catch (Exception e) {
uri = "http://www.deezer.com/track/"+track_id;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
activity.startActivity(intent);
}
However, the URI "deezer://track/track_id is never detected by Deezer and doesn't work.
Anybody knows the correct URI syntax for that ?
(I use the same code for Spotify and it works with the URI: spotify:track:track_id)
If you really want to use the deezer:// scheme, you can use the following uri :
uri = "deezer://www.deezer.com/track/"+track_id;
Edit :
You can also use some query parameters to add behavior to your link, for instance the following uri : deezer://www.deezer.com/album/10596327?autoplay=true&start_index=13 will open the deezer app on the album's page immediately and will start playing with the 14th track (0 based index).

Categories