2016-08-17 3 views
0

, dass mein Code java istTeile Text und Bild mit Android Absicht

intent.putExtra(Intent.EXTRA_SUBJECT, "My App name and some text"); 
    intent.putExtra(Intent.EXTRA_TEXT, "a link"); 
    intent.putExtra(Intent.EXTRA_STREAM,getImageUri(context,mBitmap)); 
    intent.setType("image/*,text/plain"); 
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 

Ich mag Bild und Text teilen. Dieser Code funktioniert auf WhatsApp, Twitter, Google Mail, etc .. aber es funktioniert nicht auf Facebook

Sie für Ihre Hilfe im Voraus danke

+0

Ich nehme an, dass Sie 'ACTION_SEND' verwenden, in diesem Fall gibt es zwei klare Probleme. Erstens, 'setType()' braucht keine durch Kommas getrennte Liste.Zweitens, 'ACTION_SEND' unterstützt * entweder *' EXTRA_TEXT' * oder * 'EXTRA_STREAM', nicht beides. Beide Punkte sind in [der Dokumentation zu ACTION_SEND] (https://developer.android.com/reference/android/content/Intent.html#ACTION_SEND) beschrieben. Gib deinen Code in 'intent.setType (" image/* ");' und schau, ob das hilft. Wenn dies nicht der Fall ist, bearbeiten Sie bitte Ihre Frage, um zu erklären, was auf Facebook nicht funktioniert, und geben Sie an, was 'getImageUri()' zurückgibt. – CommonsWare

+0

seit Android 4.3 setType() kann Komma-getrennte Liste und auch ACTION_SEND unterstützt EXTRA_TEXT und EXTRA_STREAM. Ich sage mein Code funktioniert für Twitter, WhatsApp und Gmail –

+0

"seit Android 4.3 setType() kann Komma-getrennte Liste nehmen" - das ist [nicht dokumentiert] (https://developer.android.com/reference/android/content /Intent.html#setType(java.lang.String)). "ACTION_SEND unterstützt EXTRA_TEXT und EXTRA_STREAM" - nicht gleichzeitig. Wenn Sie [die Dokumentation] (https://developer.android.com/reference/android/content/Intent.html#ACTION_SEND) angeben, kann "get * Extra ** entweder ** ein EXTRA_TEXT ** oder ** EXTRA_STREAM Feld haben, enthält die zu sendenden Daten Bei Verwendung von EXTRA_TEXT sollte der MIME-Typ "text/plain" sein, andernfalls sollte es der MIME-Typ der Daten in EXTRA_STREAM sein " – CommonsWare

Antwort

0

Wenn Sie das Bild und Text auf Facebook, ohne Facebook SDK teilen möchten, dann müssen Sie das Bitmap des Bildes und Text erstellen und dann können Sie das Bitmap teilen auf Facebook.

den Code Quelle von hier (Share image and text on facebook using intent in android)

activity_main.xml

<RelativeLayout android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:background="#ffffff" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <EditText 
     android:id="@+id/et_text" 
     android:layout_width="match_parent" 
     android:textSize="15dp" 
     android:layout_height="45dp" 
     android:layout_marginTop="10dp" 
     android:background="@drawable/edittext_drawable" 
     android:hint="Enter your text" 
     android:layout_marginLeft="10dp" 
     android:layout_marginRight="10dp" 
     android:paddingRight="10dp" 
     android:inputType="text" 
     android:imeOptions="actionDone" 
     android:paddingLeft="10dp" 
     android:singleLine="true" 
     android:textColorHint="#979797" /> 


    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/rl_main" 
     android:background="#ffffff" 
     android:layout_below="@+id/et_text" 
     android:layout_above="@+id/tv_share"> 


     <ImageView 
      android:layout_width="match_parent" 
      android:layout_height="250dp" 
      android:src="@drawable/index" 
      android:scaleType="fitXY" 
      android:id="@+id/iv_image" 
      android:layout_marginTop="10dp" 
      /> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:textSize="15dp" 
      android:id="@+id/tv_text" 
      android:layout_below="@+id/iv_image" 
      android:layout_margin="10dp" 
      android:textColor="#000000" 
      android:maxLines="5" 
      /> 

    </RelativeLayout> 



    <TextView 
     android:id="@+id/tv_share" 
     android:layout_width="match_parent" 
     android:layout_height="50dp" 
     android:background="#F38D0A" 
     android:gravity="center" 
     android:padding="10dp" 
     android:layout_margin="10dp" 
     android:text="Share" 
     android:textColor="#ffffff" 
     android:textSize="15dp" 
     android:layout_alignParentBottom="true"/> 

    </RelativeLayout> 

MainActivity.java

import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.net.Uri; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.util.Log; 
import android.view.View; 
import android.widget.EditText; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 
    EditText et_text; 
    ImageView iv_image; 
    TextView tv_share,tv_text; 
    RelativeLayout rl_main; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     init(); 

    } 

    private void init(){ 
     et_text = (EditText)findViewById(R.id.et_text); 
     iv_image = (ImageView)findViewById(R.id.iv_image); 
     tv_share = (TextView)findViewById(R.id.tv_share); 
     rl_main = (RelativeLayout)findViewById(R.id.rl_main); 
     tv_text= (TextView) findViewById(R.id.tv_text); 

     File dir = new File("/sdcard/Testing/"); 
     try { 
      if (dir.mkdir()) { 
       System.out.println("Directory created"); 
      } else { 
       System.out.println("Directory is not created"); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     tv_share.setOnClickListener(this); 

     et_text.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 

      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       tv_text.setText(et_text.getText().toString()); 

      } 
     }); 


    } 




    @Override 
    public void onClick(View v) { 

     switch (v.getId()){ 
      case R.id.tv_share: 
       Bitmap bitmap1 = loadBitmapFromView(rl_main, rl_main.getWidth(), rl_main.getHeight()); 
       saveBitmap(bitmap1); 
       String str_screenshot = "/sdcard/Testing/"+"testing" + ".jpg"; 

       fn_share(str_screenshot); 
       break; 
     } 

    } 

    public void saveBitmap(Bitmap bitmap) { 
     File imagePath = new File("/sdcard/Testing/"+"testing" + ".jpg"); 
     FileOutputStream fos; 
     try { 
      fos = new FileOutputStream(imagePath); 
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); 
      fos.flush(); 
      fos.close(); 

      Log.e("ImageSave", "Saveimage"); 
     } catch (FileNotFoundException e) { 
      Log.e("GREC", e.getMessage(), e); 
     } catch (IOException e) { 
      Log.e("GREC", e.getMessage(), e); 
     } 
    } 

    public static Bitmap loadBitmapFromView(View v, int width, int height) { 
     Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
     Canvas c = new Canvas(b); 
     v.draw(c); 

     return b; 
    } 

    public void fn_share(String path) { 

     File file = new File("/mnt/" + path); 

     Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath()); 
     Uri uri = Uri.fromFile(file); 
     Intent intent = new Intent(Intent.ACTION_SEND); 
     intent.setType("image/*"); 
     intent.putExtra(Intent.EXTRA_STREAM, uri); 

     startActivity(Intent.createChooser(intent, "Share Image")); 


    } 
} 

Dank herunterladen!

-1

Sie können int Wert eines Bildes als R.drawable.image teilen und bekommen es mit getResources.getDrawable (int)

1

Senden Text Content

die einfachste und gemeinsame Nutzung der ACTION_SEND Aktion Textinhalt von einer Aktivität zur anderen zu senden. Die integrierte Browser-App kann beispielsweise die URL der aktuell angezeigten Seite als Text für eine beliebige Anwendung freigeben. Dies ist nützlich, um einen Artikel oder eine Website mit Freunden über E-Mail oder soziale Netzwerke zu teilen. Hier ist der Code, diese Art des Teilens zu implementieren:

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); 

sendet Binary Inhalt

Binärdaten geteilt wird kombiniert, um die ACTION_SEND Aktion unter Verwendung mit dem entsprechenden MIME-Typ Einstellung und Platzierung der URI auf die Daten in ein Extra namens EXTRA_STREAM. Dies wird im Allgemeinen ein Bild zu teilen verwendet, sondern kann verwendet werden, um jede Art von binärem Inhalt zu teilen:

Intent shareIntent = new Intent(); 
shareIntent.setAction(Intent.ACTION_SEND); 
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage); 
shareIntent.setType("image/jpeg"); 
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to))); 

senden mehrere Stücke von Inhalt

mehrere Stücke von Inhalt zu teilen, verwenden Sie die ACTION_SEND_MULTIPLE Aktion zusammen mit einer Liste von URIs, die auf den Inhalt zeigen. Der MIME-Typ hängt von der Mischung der Inhalte ab, die Sie teilen. Wenn Sie beispielsweise 3 JPEG-Bilder teilen, ist der Typ immer noch "image/jpeg". Bei einer Mischung von Bildtypen sollte es "image/" sein, um mit einer Aktivität übereinzustimmen, die jede Art von Bild verarbeitet. Sie sollten nur "/*" verwenden, wenn Sie eine Vielzahl von Typen teilen. Wie bereits erwähnt, liegt es an der empfangenden Anwendung, Ihre Daten zu analysieren und zu verarbeiten. Hier ein Beispiel:

ArrayList<Uri> imageUris = new ArrayList<Uri>(); 
imageUris.add(imageUri1); // Add your image URIs here 
imageUris.add(imageUri2); 

Intent shareIntent = new Intent(); 
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE); 
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, imageUris); 
shareIntent.setType("image/*"); 
startActivity(Intent.createChooser(shareIntent, "Share images to..")); 

For More Info Please Visit Official Document

Verwandte Themen