2011-04-05 17 views
9

quizás una pregunta fácil: quiero compartir un mapa de bits que recibí en la red a twitter/facebook/etc con el "intento" de compartir predeterminado.Compartir mapa de bits() en android a twitter, facebook, mail

El código que encontró

 Intent sendIntent = new Intent(Intent.ACTION_SEND); 
     sendIntent.setType("image/jpeg"); 
     sendIntent.putExtra(Intent.EXTRA_STREAM, "IDONTKNOW"); 
     sendIntent.putExtra(Intent.EXTRA_TEXT, 
       "See my captured picture - wow :)"); 
     startActivity(Intent.createChooser(sendIntent, "share")); 

necesidades a ser llenados en el punto "idontknow" con el mapa de bits. (This.bitmap)

he encontrado ninguna manera de manejar esto sin guardar el mapa de bits a SD interna ..

respecto

+0

Este Q & A es la pena leer http://stackoverflow.com/questions/9049143/android -share-intent-for-a-bitmap-is-it-possible-not-to-save-it-prior-sharing – Suragch

Respuesta

7

Simplemente, puede convertir un mapa de bits en PNG del almacenamiento externo.

File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
File imageFile = new File(path, getCurrentTime()+ ".png"); 
FileOutputStream fileOutPutStream = new FileOutputStream(imageFile); 
bitmap.compress(Bitmap.CompressFormat.PNG, 80, fileOutPutStream); 

fileOutPutStream.flush(); 
fileOutPutStream.close(); 

Entonces, se puede obtener una URI a través Uri.parse:

return Uri.parse("file://" + imageFile.getAbsolutePath()); 
+0

String path = Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES); –

1

Ok lo tengo por mi cuenta, parece que no hay manera de conseguir el URI imagen sin guardar el mapa de bits en el disco, por lo tanto, utilizar este método simple:

private Uri storeImage() { 
    this.storedImage = null; 
    this.storeImage = true; 
    // Wait for the image 
    while (this.storedImage == null && !this.stop) 
     try { 
      Thread.sleep(100); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    this.storeImage = false; 
    FileOutputStream fileOutputStream = null; 
    File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 
    File file = new File(path, "cwth_" + getCurrentTime()+ ".jpg"); 
    try { 
     fileOutputStream = new FileOutputStream(file); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 
    BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream); 
    this.storedImage.compress(CompressFormat.JPEG, JPEG_STORE_QUALITY, bos); 
    try { 
     bos.flush(); 
     bos.close(); 
     fileOutputStream.flush(); 
     fileOutputStream.close(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return Uri.parse("file://" + file.getAbsolutePath()); 
} 
4

podría ser un poco tarde, pero también se podría hacer String url = Images.Media.insertImage(context.getContentResolver(), image, "title", null); si no le importa cómo se almacena.

+0

Este método crea una miniatura para la imagen (como documentación). – wisemann

-2

enviar contenido binario Los datos binarios se comparte mediante la acción combinada con ACTION_SEND establecer el tipo MIME adecuado y la colocación de la URI los datos en un extra llamado EXTRA_STREAM. Esto es comúnmente utilizado para compartir una imagen, pero se puede utilizar para compartir cualquier tipo de contenido binario:

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

fuente http://developer.android.com/training/sharing/send.html

Cuestiones relacionadas