2014-10-10 9 views
6

Non ho potuto fare a meno di notare che tutti gli esempi di condivisione di immagini con Intent utilizzano file memorizzati localmente. Ogni volta che provo a usare un URL esterno, facebook, twitter ecc. Mi dà un brindisi dicendo "Uno o più elementi multimediali potrebbero non essere aggiunti".Condividi intenzioni Android utilizzando l'url immagine esterna

Devo memorizzare una copia dell'immagine localmente? Se sì, come faccio?

+0

Eventuali duplicati [immagine quota di Android di url] (di https://stackoverflow.com/questions/16300959/android-share -image-from-url) –

risposta

4

Ecco come è possibile condividere un link:

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

Il messaggio di brindisi non viene più visualizzato ma l'immagine non verrà aggiunta. A quale app stai condividendo? – user2799221

+0

Sì, l'immagine non funziona – Pavlos

17

Grazie @ user2245247 per la link
Esso contiene la risposta giusta per condividere immagini da URL remoto .
Usa libreria esterna

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

Dopo che l'immagine caricata con successo alla visualizzazione dell'immagine, innescare il metodo per la condivisione di intenti.

// 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; 
} 

Accertarsi che siano presenti le seguenti autorizzazioni date.

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

Risposta straordinaria che ha funzionato per me –

+3

Che dire se non voglio mostrarlo a qualsiasi ImageView? – santafebound

+0

Lo sto usando in un recyclerview e fa invece riferimento all'immagine precedente. Qualche consiglio? –

-1

Un altro modo alternativo per raggiungere questo obiettivo con l'utilizzo method from previous answer è:

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; 
} 
Problemi correlati