2015-10-19 16 views
7

Nella mia app Android, ho una bitmap (ad esempio b) e un pulsante. Ora quando clicco sul pulsante, voglio condividere la bitmap. Sto facendo uso del codice di seguito all'interno del mio onClick() per raggiungere questo obiettivo: -Condivisione di bitmap tramite Android Intent

Intent intent = new Intent(Intent.ACTION_SEND); 
intent.setType("image/png"); 
intent.putExtra(Intent.EXTRA_STREAM, b); 
startActivity(Intent.createChooser(intent , "Share")); 

mi aspettavo un elenco di tutte le applicazioni che sono in grado di gestire questo intento, ma non ottengo niente. Non ci sono elenchi di app né errori in Android Studio. La mia applicazione viene semplicemente impiccata per qualche tempo e poi si chiude.

Ho controllato la bitmap e va bene (non è nulla).

Dove sto andando male?

risposta

4

Citando the documentation:

Un contenuto: URI che tiene un flusso di dati associati con l'intento, usati con ACTION_SEND di fornire i dati inviati.

b, quindi, non dovrebbe essere un Bitmap, ma piuttosto un Uri che punta a un Bitmap, servito da un ContentProvider. Ad esempio, è possibile scrivere Bitmap in un file, quindi utilizzare FileProvider per servirlo.

11

Come dichiarato da CommonsWare, è necessario ottenere l'URI nella bitmap e passarlo come Extra.

String bitmapPath = Images.Media.insertImage(getContentResolver(), bitmap,"title", null); 
Uri bitmapUri = Uri.parse(bitmapPath); 
... 
intent.putExtra(Intent.EXTRA_STREAM, bitmapUri); 
+0

perfettamente funzionante –

6

** Finalmente ho avuto la soluzione **

Fase 1:. Condividi la manipolazione Block Intent. Questo aprirà la finestra con l'elenco delle applicazioni nel telefono

public void share_bitMap_to_Apps() { 

    Intent i = new Intent(Intent.ACTION_SEND); 

    i.setType("image/*"); 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
    /*compress(Bitmap.CompressFormat.PNG, 100, stream); 
    byte[] bytes = stream.toByteArray();*/ 


    i.putExtra(Intent.EXTRA_STREAM, getImageUri(mContext, getBitmapFromView(relative_me_other))); 
    try { 
     startActivity(Intent.createChooser(i, "My Profile ...")); 
    } catch (android.content.ActivityNotFoundException ex) { 

     ex.printStackTrace(); 
    } 


} 

Fase 2: Conversione immagine per bitmap

public static Bitmap getBitmapFromView(View view) { 
    //Define a bitmap with the same size as the view 
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(),  view.getHeight(), Bitmap.Config.ARGB_8888); 
    //Bind a canvas to it 
    Canvas canvas = new Canvas(returnedBitmap); 
    //Get the view's background 
    Drawable bgDrawable = view.getBackground(); 
    if (bgDrawable != null) 
     //has background drawable, then draw it on the canvas 
     bgDrawable.draw(canvas); 
    else 
     //does not have background drawable, then draw white background on the canvas 
     canvas.drawColor(Color.WHITE); 
    // draw the view on the canvas 
    view.draw(canvas); 
    //return the bitmap 
    return returnedBitmap; 
} 

Fase 3:

Per ottenere l'URI dall'immagine bitmap

public Uri getImageUri(Context inContext, Bitmap inImage) { 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 

    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null); 
    return Uri.parse(path); 
} 
+1

non dimenticate di richiedere android.permission.WRITE_EXTERNAL_STORAGE – appsthatmatter

2
ImageButton capture_share = (ImageButton) findViewById(R.id.share); 
capture_share.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 

    String bitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap,"title", null); 
    Uri bitmapUri = Uri.parse(bitmapPath); 

     Intent intent = new Intent(Intent.ACTION_SEND); 
     intent.setType("image/png"); 
     intent.putExtra(Intent.EXTRA_STREAM, bitmapUri); 
     startActivity(Intent.createChooser(intent, "Share")); 



    } 
}); 
+0

Aggiungere qualche descrizione di cui rispondere chiara comprensione. @lalit Baghel –

Problemi correlati