show JUST ONCE and ALWAYS option using Intent.createChooser - android

I open chooser dialog using Intent.createChooser() method. But in that dialog I can't see "JUST ONCE" and "ALWAYS" options.
I want this type of chooser dialog with Intent.createChooser() method
here is my code.
File mFile = new File("give your file path here");
String type = "video/mp4";
Intent viewDoc = new Intent(Intent.ACTION_VIEW);
viewDoc.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
viewDoc.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(this, "com.xyz.fileprovider", mFile);
viewDoc.setDataAndType(contentUri, type);
PackageManager pm = getPackageManager();
apps = pm.queryIntentActivities(viewDoc, PackageManager.MATCH_DEFAULT_ONLY);
ArrayList<Intent> targetIntents = new ArrayList<Intent>();
for (ResolveInfo resolveInfo : apps) {
String packageName = resolveInfo.activityInfo.packageName;
grantUriPermission(packageName, contentUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (!BuildConfig.APPLICATION_ID.equals(packageName)) {
Intent targetIntent = new Intent(Intent.ACTION_VIEW); // targetIntent.setDataAndType(contentUri, type);
targetIntent.setPackage(packageName);
targetIntent.setDataAndType(contentUri, type);
targetIntents.add(targetIntent);
}
}
if (type == null || type.equals("") || targetIntents.size() <= 0) { //application/octet-stream
android.app.AlertDialog alertDialog = new android.app.AlertDialog.Builder(this).create();
alertDialog.setTitle(getResources().getString(R.string.app_name));
alertDialog.setMessage(getResources().getString(R.string.offline_file_not_supported_error));
alertDialog.setButton(DialogInterface.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alertDialog.show();
} else {
Intent chooserIntent = Intent.createChooser(targetIntents.remove(0), "Open file with");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
//startActivity(viewDoc);
}
But after this code I get chooser dialog like below
But i need JUST ONCE and ALWAYS option in this dialog.
I used below mentioned code because I want to remove my own app from chooser.
for (ResolveInfo resolveInfo : apps) {
String packageName = resolveInfo.activityInfo.packageName;
grantUriPermission(packageName, contentUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (!BuildConfig.APPLICATION_ID.equals(packageName)) {
Intent targetIntent = new Intent(Intent.ACTION_VIEW); // targetIntent.setDataAndType(contentUri, type);
targetIntent.setPackage(packageName);
targetIntent.setDataAndType(contentUri, type);
targetIntents.add(targetIntent);
}
}

Related

Android - Why can't I start the choosePictureIntent?

Im trying to start the choosePictureIntent. This is done by clicking on an image in an Ap
#Override
public void imageClicked(int position) {
if(data.get(position).getImgUri() != null) {
// IGNORE THIS
Intent intent= new Intent(context, FullScreenActivity.class);
intent.putExtra("data", data.get(position));
context.startActivity(intent);
} else {
// THIS IS THE CODE
Object[] chooseData;
chooseData = Utils.getChoosePictureIntent(context, context.getPackageManager());
Intent chooserIntent = (Intent) chooseData[0];
chooserIntent.putExtra("data", data.get(position)); // data is an array of information
chooserIntent.putExtra("outputFileUri", (Uri) chooseData[1]);
((Activity) context).startActivityForResult(chooserIntent, PICTURE_REQUEST);
}
}
When I click on the image, nothing happens, but I know that the else statement gets executed. After that, if I click on the view besides the image, the whole app freezes.
There are no error messages.
Any help is appreciated.
The source of getChoosePictureIntent():
public static String getUniqueFileName(String prefix, String surfix) {
return prefix + System.currentTimeMillis() + surfix;
}
public static Object[] getChoosePictureIntent(Context context, PackageManager manager) {
final File root = new File(Environment.getExternalStorageDirectory() + File.separator + "imagedir" + File.separator);
root.mkdirs();
final String fName = Utils.getUniqueFileName("img_purchase_", ".jpg");
final File sdImageMainDirectory = new File(root, fName);
Uri outputFileUri = Uri.fromFile(sdImageMainDirectory);
// Camera.
final List<Intent> cameraIntents = new ArrayList<Intent>();
final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final PackageManager packageManager = manager;
final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
for(ResolveInfo res : listCam) {
final String packageName = res.activityInfo.packageName;
final Intent intent = new Intent(captureIntent);
intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
cameraIntents.add(intent);
}
// Filesystem.
final Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Chooser of filesystem options.
final Intent chooserIntent = Intent.createChooser(galleryIntent, context.getString(R.string.picture_chooser));
// Add the camera options.
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));
return new Object[]{chooserIntent, outputFileUri};
}
If you are using targetSdk >=24, then you need to add File Provider in order to replace the File Uri received from Camera App to Content Uri because API level 24 has blocked File Uri from another app for security reason.You can check out this link for more info

android filter app returns two twitter

I am trying to filter the app i have in the phone, facebook and twitter are what I want to find. Below is the code i use, and it works fine before, it filter twitter and facebook for me to post something. Then later it become i filter two twitter apps. Anyone know why?
I use debug more and found these, how come i get two twitter package. I have tried delete and reinstall my twitter app, it won't work.
ResolveInfo{445f8600 com.twitter.android/.composer.ComposerActivity m=0x608000}
ResolveInfo{445f14d0 com.twitter.android/.DMActivity m=0x608000}
private void shareContent() {
UtuBaseActivity activity = (UtuBaseActivity) getActivity();
if (activity == null || activity.isFinishing())
return;
if (promotionDetail == null)
return;
String title = getResources().getString(R.string.share_chooser_title);
String app_name = getResources().getString(R.string.app_name);
List<Intent> targetShareIntents = new ArrayList<>();
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
List<ResolveInfo> resInfos = activity.getPackageManager().queryIntentActivities(shareIntent, 0);
if (!resInfos.isEmpty()) {
for (ResolveInfo resInfo : resInfos) {
String packageName = resInfo.activityInfo.packageName;
if (packageName.contains("com.twitter.android") || packageName.contains("com.facebook.katana")) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, resInfo.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, app_name);
intent.putExtra(Intent.EXTRA_TEXT, promotionDetail.getCoverpicture());
intent.setPackage(packageName);
targetShareIntents.add(intent);
}
}
if (!targetShareIntents.isEmpty()) {
Intent chooserIntent = Intent.createChooser(targetShareIntents.remove(0), title);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
} else {
// As fallback, launch sharer.php in a browser
String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + promotionDetail.getCoverpicture();
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
startActivity(i);
}
}
}
add this, it works tested.
check parent name equals to com.twitter.android.MainActivity
private void shareContent() {
UtuBaseActivity activity = (UtuBaseActivity) getActivity();
if (activity == null || activity.isFinishing())
return;
if (promotionDetail == null)
return;
String title = getResources().getString(R.string.share_chooser_title);
String app_name = getResources().getString(R.string.app_name);
List<Intent> targetShareIntents = new ArrayList<>();
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
List<ResolveInfo> resInfos = activity.getPackageManager().queryIntentActivities(shareIntent, 0);
if (!resInfos.isEmpty()) {
for (ResolveInfo resInfo : resInfos) {
String packageName = resInfo.activityInfo.packageName;
String parentName = resInfo.activityInfo.parentActivityName;
if (packageName.contains("com.twitter.android") || packageName.contains("com.facebook.katana")) {
if (packageName.contains("com.twitter.android"))
if (!parentName.equals("com.twitter.android.MainActivity"))
continue;
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, resInfo.activityInfo.name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, app_name);
intent.putExtra(Intent.EXTRA_TEXT, promotionDetail.getCoverpicture());
intent.setPackage(packageName);
targetShareIntents.add(intent);
}
}
if (!targetShareIntents.isEmpty()) {
Intent chooserIntent = Intent.createChooser(targetShareIntents.remove(0), title);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetShareIntents.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
} else {
// As fallback, launch sharer.php in a browser
String sharerUrl = "https://www.facebook.com/sharer/sharer.php?u=" + promotionDetail.getCoverpicture();
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(sharerUrl));
startActivity(i);
}
}
}

Camera intent doesn't show in intent chooser

String chooseTitle = activity.getString(R.string.select_or_take_picture);
Intent getIntent = new Intent();
getIntent.setType("image/*");
getIntent.setAction(Intent.ACTION_GET_CONTENT);
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent.setType("image/*");
PackageManager pm = activity.getApplicationContext().getPackageManager();
for (ResolveInfo ri: pm.queryIntentActivities(galleryIntent, PackageManager.MATCH_DEFAULT_ONLY)) {
Intent intent = pm.getLaunchIntentForPackage(ri.activityInfo.packageName);
intent.setAction(Intent.ACTION_PICK);
intents.add(intent);
}
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, photoUri);
for (ResolveInfo ri : pm.queryIntentActivities(cameraIntent, 0)) {
Intent intent = pm.getLaunchIntentForPackage(ri.activityInfo.packageName);
intents.add(intent);
}
Intent chooserIntent = Intent.createChooser(getIntent, chooseTitle);
chooserIntent.putExtra(
Intent.EXTRA_INITIAL_INTENTS,
intents.toArray(new Parcelable[] {})
);
By doing this way, the chooser shows:
The camera intent doesn't show at all.
But if I change the line
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, photoUri);
to
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
It works just fine:
But the problem is, I really want to pass photoUri. How can I do with it?
I know a possible alternative is to write my own chooser dialog, but I do want to know if it's a bug in intent chooser, or if I don't use it correctly.
p.s.
#dkarmazi, Here's how I generate Uri:
public Uri generatePhotoUri() {
String timeStamp = new SimpleDateFormat("yyyyMMdd-HHmmss").format(new Date());
String imageFileName = "XXX_" + timeStamp + ".jpg";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File imageFile = new File(storageDir, imageFileName);
return Uri.fromFile(imageFile);
}
Here's my onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case PICK_PHOTO:
if (resultCode != Activity.RESULT_OK) {
break;
}
Uri source = data == null ?
mPhotoUri : // take picture
data.getData(); // choose from other app
if (source == null) {
break;
}
// TODO: do with source
break;
}
//....
}
#dkarmazi, I've debugged it, and I make sure resultCode is RESULT_CANCELED.
I recently worked on the same problem and here is my solution:
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
Then, once the user takes a picture, you should be able to access it by using the provided photoUri
Here is some documentation on EXTRA_OUTPUT and you can also lookup ACTION_IMAGE_CAPTURE on the same page.
UPDATE on Intent Chooser:
// we create intent chooser by picking one of the intents
Intent chooserIntent = Intent.createChooser(cameraIntent, getResources().getString(R.string.pick_action_string_for_user));
// then we add any additional intents
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { getIntent });
// chooserIntent is ready
startActivityForResult(chooserIntent, requestCode);
Thank #dkarmazi very much for helping me debug this issue. It seems we haven't enough reputation to refine the answer together, so I'm posting the solution here. Later in our chat we find a probable cause, and after my experimenting, now it works. It's actually an issue how I gather cameraIntents, so change this part of code
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE, photoUri);
for (ResolveInfo ri : pm.queryIntentActivities(cameraIntent, 0)) {
Intent intent = pm.getLaunchIntentForPackage(ri.activityInfo.packageName);
intents.add(intent);
}
to
final Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
for(ResolveInfo ri : pm.queryIntentActivities(cameraIntent, 0)) {
final String packageName = ri.activityInfo.packageName;
final Intent intent = new Intent(cameraIntent);
intent.setComponent(new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name));
intent.setPackage(packageName);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
intents.add(intent);
}
Also need to change
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
galleryIntent.setType("image/*");
PackageManager pm = activity.getApplicationContext().getPackageManager();
for (ResolveInfo ri: pm.queryIntentActivities(galleryIntent, PackageManager.MATCH_DEFAULT_ONLY)) {
Intent intent = pm.getLaunchIntentForPackage(ri.activityInfo.packageName);
intent.setAction(Intent.ACTION_PICK);
intents.add(intent);
}
to
Intent galleryIntent = new Intent(Intent.ACTION_PICK);
galleryIntent.setType("image/*");
PackageManager pm = activity.getApplicationContext().getPackageManager();
for (ResolveInfo ri: pm.queryIntentActivities(galleryIntent, PackageManager.MATCH_DEFAULT_ONLY)) {
final String packageName = ri.activityInfo.packageName;
final Intent intent = new Intent(galleryIntent);
intent.setComponent(new ComponentName(ri.activityInfo.packageName, ri.activityInfo.name));
intent.setPackage(packageName);
intents.add(intent);
}
I hate the boilerplate of it, but finally it works.

Customize Android Intent.ACTION_SEND

I'm using Intent for sharing url and subject. In this intent filter showing all the sharing apps. i want only (facebook/gmail/message/skype/twitter) these option in popup. is this possible to customize sharing intent filter.
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = adapter.getDetails("url";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"subject");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
thanks
Yes, its possible Check out below which shows the filteration for Facebook,Gmail,Twitter.
Updated to Share Text + Image:
Select the image from the SDCard:
String fileName = "image-3116.jpg";
String externalStorageDirectory = Environment.getExternalStorageDirectory().toString();
String myDir = externalStorageDirectory + "/saved_images/"; // the
// file will be in saved_images
Uri uri = Uri.parse("file:///" + myDir + fileName);
Share via Twitter
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String) v.getTag(R.drawable.ic_launcher));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ("com.twitter.android.PostActivity".equals(app.activityInfo.name))
{
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
Share via Facebook
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String) v.getTag(R.drawable.ic_launcher));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ((app.activityInfo.name).startsWith("com.facebook.katana"))
{
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
Share via Gmail
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String) v.getTag(R.drawable.ic_launcher));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList)
{
if ((app.activityInfo.name).contains("android.gm"))
{
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
Share via WhatsApp:
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setType("text/html");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, (String) v.getTag(R.string.app_name));
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, (String) v.getTag(R.drawable.ic_launcher));
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(shareIntent, 0);
for (final ResolveInfo app : activityList) {
if ((app.activityInfo.name).contains("com.whatsapp")) {
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(
activity.applicationInfo.packageName, activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
Single method for multiple functionalities
Code of share(String nameApp,String imagePath,String message) function:
public void share(String nameApp, String imagePath, String message) {
try {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg");
List<ResolveInfo> resInfo = getPackageManager()
.queryIntentActivities(share, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
Intent targetedShare = new Intent(
android.content.Intent.ACTION_SEND);
targetedShare.setType("image/jpeg"); // put here your mime
// type
if (info.activityInfo.packageName.toLowerCase().contains(
nameApp)
|| info.activityInfo.name.toLowerCase().contains(
nameApp)) {
targetedShare.putExtra(Intent.EXTRA_SUBJECT,
"Sample Photo");
targetedShare.putExtra(Intent.EXTRA_TEXT, message);
targetedShare.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(new File(imagePath)));
targetedShare.setPackage(info.activityInfo.packageName);
targetedShareIntents.add(targetedShare);
}
}
Intent chooserIntent = Intent.createChooser(
targetedShareIntents.remove(0), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
targetedShareIntents.toArray(new Parcelable[] {}));
startActivity(chooserIntent);
}
} catch (Exception e) {
Log.v("VM",
"Exception while sending image on" + nameApp + " "
+ e.getMessage());
}
}
For attaching image on gmail, facebook, twitter with text use below code.
File filePath = new File("your image path");
share("gmail", filePath.toString(),"your comment");
share("facebook", filePath.toString(),"your comment");
share("twitter", filePath.toString(),"your comment");
Above given answer open facebook chat app on my phone. For the following worked !!
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
sharingIntent.putExtra(Intent.EXTRA_TEXT, "Text...");
PackageManager packManager = getPackageManager();
List<ResolveInfo> resolvedInfoList = packManager.queryIntentActivities(sharingIntent, PackageManager.MATCH_DEFAULT_ONLY);
boolean resolved = false;
for(ResolveInfo resolveInfo: resolvedInfoList){
if(resolveInfo.activityInfo.packageName.startsWith("com.facebook.katana")){
sharingIntent.setClassName(
resolveInfo.activityInfo.packageName,
resolveInfo.activityInfo.name );
resolved = true;
break;
}
}
if(resolved){
startActivity(sharingIntent);
}else{
Builder alert = new AlertDialog.Builder(ActivityName.this);
alert.setTitle("Warning");
alert.setMessage("Facebook App not found");
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which)
{
dialog.dismiss();
}
});
alert.show();
}
You can't share Extras with image without using facebook sdk.
https://developers.facebook.com/x/bugs/332619626816423/

android sharing application

I have a few problems with my android app. I want to share some text and photo to fb and twitter. The first problem is that i can't copy the text from my textfield to the fb message ... i mean i have a text field with the text you want to share and 2 buttons ( fb, twitter) and the twitter side is working fine but i can't attach the text to the fb message ( the window appears on the push of the button but it's blank). Here is my code for sharing on facebbok:
Intent shareIntent = new Intent(
android.content.Intent.ACTION_SEND);
shareIntent.setType("text/plain");
shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
txt.getText());
shareIntent.putExtra(android.content.Intent.EXTRA_TEXT,
txt.getText());
PackageManager pm = v.getContext().getPackageManager();
List<ResolveInfo> activityList = pm.queryIntentActivities(
shareIntent, 0);
for (final ResolveInfo app : activityList) {
if ((app.activityInfo.name).contains("facebook")) {
final ActivityInfo activity = app.activityInfo;
final ComponentName name = new ComponentName(
activity.applicationInfo.packageName,
activity.name);
shareIntent.addCategory(Intent.CATEGORY_LAUNCHER);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
shareIntent.setComponent(name);
v.getContext().startActivity(shareIntent);
break;
}
}
My other problem is that my photo browser is giving me a path of the file like this: "external/images/media/photo" and i though that the problem is that i have to copy the extension too (.jpg etc.) but that's not it. If i use a direct path: "/mnt/sdcard/DCIM/01.jpg" it's working fine. Can someone help me how to find the direct path of the photo or how can i modify my code that it works with the first one:
private void share(String nameApp, String imagePath, String text) {
try {
List<Intent> targetedShareIntents = new ArrayList<Intent>();
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg");
List<ResolveInfo> resInfo = getPackageManager()
.queryIntentActivities(share, 0);
if (!resInfo.isEmpty()) {
for (ResolveInfo info : resInfo) {
Intent targetedShare = new Intent(
android.content.Intent.ACTION_SEND);
targetedShare.setType("image/jpeg");
if (info.activityInfo.packageName.toLowerCase().contains(
nameApp)
|| info.activityInfo.name.toLowerCase().contains(
nameApp)) {
targetedShare.putExtra(Intent.EXTRA_SUBJECT, text);
targetedShare.putExtra(Intent.EXTRA_TEXT, text);
targetedShare.putExtra(Intent.EXTRA_STREAM,
Uri.fromFile(new File(imagePath)));
targetedShare.setPackage(info.activityInfo.packageName);
targetedShareIntents.add(targetedShare);
}
}
Intent chooserIntent = Intent.createChooser(
targetedShareIntents.remove(0), "Select app to share");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
targetedShareIntents.toArray(new Parcelable[] {}));
startActivity(chooserIntent);
}
} catch (Exception e) {
}
}
Thx anticipated ...
You can get the exact path of your image as below, I have already shared the photo & text on FB & Twitter the way you have shared and its working fine for me also. Hope this will help you.
EDITED:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}

Categories

Resources