Condivisione su Twitter e Facebook tramite app Android
Sto sviluppando un'applicazione adroid con cui posso condividere il text (e anche foto successive) tramite facebook e twitter. Ho trovato un codice che sta aprendo la window di condivisione di facebook / twitter, ma il text che deve essere condiviso è in un EditText nell'app, quindi la mia domanda è come posso inserire il text dal text EditText al text che verrà condiviso (mi dispiace per il cattivo inglese se hai bisogno di ulteriori spiegazioni o non hai capito nulla poi cercherò anche di spiegarlo meglio). Questo è il mio codice per entrambi i methods di condivisione:
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)); 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; } }
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)); 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; } }
2 Solutions collect form web for “Condivisione su Twitter e Facebook tramite app Android”
Utilizza questo metodo per condividere foto con il text. Chiamare questo metodo e passare l'argomento nameofapp e imagepath. Il nome di app significa quello su cui si desidera condividere come gmail, facebook, twitter.
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"); // put here your mime type 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){ } }
Utilizza questo per facebook: –
try { File filePath = "/mnt/sdcard/vmphoto.jpg"; //This is imagefile path in your change it acc. to your requirement. share("facebook",filePath.toString(),"Hello"); <<<<<Hello is text. send acc. to you req. } catch(Exception e) { //exception occur might your app like gmail , facebook etc not installed or not working correctly. }
Per twitter
try { File filePath = "/mnt/sdcard/vmphoto.jpg"; //This is imagefile path in your change it acc. to your requirement. share("twitter",filePath.toString(),"Hello"); <<<<<Hello is a text send acc. to you req. } catch(Exception e) { //exception occur might your app like gmail , facebook etc not installed or not working correctly. }
Per albind l'image su gmail, facebook, twitter con codice di text sotto il codice.
File filePath = context.getFileStreamPath("vmphoto.jpg"); share("gmail",filePath.toString()); share("facebook",filePath.toString()); share("twitter",filePath.toString());
// codice della condivisione (String nameApp, String imagePath)
void share(String nameApp, String imagePath) { 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,"This photo is created by App Name"); 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()); } }