2014-10-10 3 views
6

ich nicht umhin zu bemerken konnte, dass alle Beispiele für den Austausch von Bildern mit Intent Verwendung lokal Dateien gespeichert. Jedes Mal, wenn ich versuche, eine externe URL zu verwenden, facebook, twitter und gibt mir einen Toast sagen: „Ein oder mehr Medien Elemente können nicht hinzugefügt werden, werden“.Android Intent Teile mit externem Bild-URL

Muss ich lokal eine Kopie des Bildes speichern haben? Wenn ja, wie kann ich das tun?

+0

Mögliches Duplikat [android Aktie Bild von url] (https://stackoverflow.com/questions/16300959/android-share -Image-from-url) –

Antwort

4

Hier ist, wie Sie einen Link teilen können:

Intent intent = new Intent(Intent.ACTION_SEND); 
Uri uri = Uri.parse("http://linkto.com/your_image.png"); 
intent.setType("image/*"); 
intent.putExtra(Intent.EXTRA_STREAM, String.valueOf(uri)); 
startActivity(intent); 
+3

der Toast Meldung nicht mehr, aber das Bild wird nicht hinzufügen. Mit welcher App teilen Sie? – user2799221

+0

Ja wird das Bild nicht funktioniert – Pavlos

17

Danke @ user2245247 für die link
Es enthält die richtige Antwort Bild von Remote-URL zu teilen .
Verwenden externe Bibliothek

// Get access to ImageView 
ImageView ivImage = (ImageView) findViewById(R.id.ivResult); 
// Fire async request to load image 
Picasso.with(context).load(imageUrl).into(ivImage); 

Nachdem das Bild erfolgreich zu der Bildaufnahme, löst die Verfahren für den Austausch von Absicht geladen.

// Can be triggered by a view event such as a button press 
public void onShareItem(View v) { 
    // Get access to bitmap image from view 
    ImageView ivImage = (ImageView) findViewById(R.id.ivResult); 
    // Get access to the URI for the bitmap 
    Uri bmpUri = getLocalBitmapUri(ivImage); 
    if (bmpUri != null) { 
     // Construct a ShareIntent with link to image 
     Intent shareIntent = new Intent(); 
     shareIntent.setAction(Intent.ACTION_SEND); 
     shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri); 
     shareIntent.setType("image/*"); 
     // Launch sharing dialog for image 
     startActivity(Intent.createChooser(shareIntent, "Share Image"));  
    } else { 
     // ...sharing failed, handle error 
    } 
} 

// Returns the URI path to the Bitmap displayed in specified ImageView 
public Uri getLocalBitmapUri(ImageView imageView) { 
    // Extract Bitmap from ImageView drawable 
    Drawable drawable = imageView.getDrawable(); 
    Bitmap bmp = null; 
    if (drawable instanceof BitmapDrawable){ 
     bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap(); 
    } else { 
     return null; 
    } 
    // Store image to default external storage directory 
    Uri bmpUri = null; 
    try { 
     File file = new File(Environment.getExternalStoragePublicDirectory( 
      Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png"); 
     file.getParentFile().mkdirs(); 
     FileOutputStream out = new FileOutputStream(file); 
     bmp.compress(Bitmap.CompressFormat.PNG, 90, out); 
     out.close(); 
     bmpUri = Uri.fromFile(file); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return bmpUri; 
} 

Stellen Sie sicher, dass Sie die folgenden Berechtigungen haben.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
+0

Ehrfürchtig Antwort, die für mich gearbeitet –

+3

Was ist, wenn ich nicht will, es zu jedem Image zeigen? – santafebound

+0

Ich benutze dies in einer Recyclerview und es verweist stattdessen auf das vorherige Bild. Irgendwelche Tipps? –

-1

Eine weitere alternative Möglichkeit, diese method from previous answer bei der Verwendung zu erreichen ist:

Picasso.with(this).load(imageurl).into(shareTarget); 

private Target shareTarget = new Target() { 
    @Override 
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) { 
     // Get access to the URI for the bitmap 
     Uri bmpUri = getLocalBitmapUri(bitmap); 
     if (bmpUri != null) { 
      // Construct a ShareIntent with link to image 
      Intent shareIntent = new Intent(); 
      shareIntent.setAction(Intent.ACTION_SEND); 
      shareIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.share_text)); 
      shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri); 
      shareIntent.setType("image/*"); 
      // Launch sharing dialog for image 
      startActivity(Intent.createChooser(shareIntent, "Share Image")); 
     } else { 
      // ...sharing failed, handle error 
     } 
    } 

    @Override 
    public void onBitmapFailed(Drawable errorDrawable) { 
    } 

    @Override 
    public void onPrepareLoad(Drawable placeHolderDrawable) { 

    } 
}; 

public Uri getLocalBitmapUri(Bitmap bmp) { 
    // Store image to default external storage directory 
    Uri bmpUri = null; 
    try { 
     File file = new File(Environment.getExternalStoragePublicDirectory(
       Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".png"); 
     file.getParentFile().mkdirs(); 
     FileOutputStream out = new FileOutputStream(file); 
     bmp.compress(Bitmap.CompressFormat.PNG, 90, out); 
     out.close(); 
     bmpUri = Uri.fromFile(file); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return bmpUri; 
}