digitaldaimyo
digitaldaimyo

Reputation: 21

Android Eclipse: Sharing image to facebook without mediaStore.Insert()

I would like to share a screenshot to Facebook, Twitter, etc, using an Intent. I have found the code examples that achieve this by inserting the image into media store and then grabbing the URI to this image. However, I do not want to clog the user's device with these useless images, and I also do not want to ask for the Access External permission, as i do not use it anywhere else in the app. Is there a way to build the intent such that it will share an image without having to have a URI? like passing the bitmap directly, or passing it after compressing it to .png? or is there a way to build a URI to a bitmap held in memory, without first making it a file?

I am using this now:

public void shareScore(String scoreTextString, String scoreString, Uri screenShotUri){      
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);      
        sharingIntent.setType("image/png");     
        sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM, screenShotUri);     
        startActivity(Intent.createChooser(sharingIntent, "Share Via..."));
    }

public Bitmap takeScreenshot(GL10 mGL) {        
    final int mWidth =  this.getGlView().getWidth();
    final int mHeight = this.getGlView().getHeight();
    IntBuffer ib = IntBuffer.allocate(mWidth * mHeight);
    IntBuffer ibt = IntBuffer.allocate(mWidth * mHeight);
    mGL.glReadPixels(0, 0, mWidth, mHeight, GL10.GL_RGBA, GL10.GL_UNSIGNED_BYTE, ib);

    // Convert upside down mirror-reversed image to right-side up normal image.
    for (int i = 0; i < mHeight; i++) {
        for (int j = 0; j < mWidth; j++) {
            ibt.put((mHeight - i - 1) * mWidth + j, ib.get(i * mWidth + j));
        }
    }       

    Bitmap mBitmap = Bitmap.createBitmap(mWidth, mHeight,Bitmap.Config.ARGB_8888);
    mBitmap.copyPixelsFromBuffer(ibt);      
    return mBitmap;
}

private Uri getImageUri(Context inContext, Bitmap inImage) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.PNG, 100, bytes);
        String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);     
        return Uri.parse(path);
    }

private void share(){
    String scoreTextString = "I set a new high score on Butterfly Bonanza!";
    String scoreString = "Score :" + Integer.toString(score);               
    Uri screenShotUri = getImageUri(GLGame.class.cast(game), ButterflyBonanza.class.cast(game).getScreenShot());
    ButterflyBonanza.class.cast(game).shareScore(scoreTextString, scoreString, screenShotUri);
}

Upvotes: 1

Views: 549

Answers (1)

Manuel Allenspach
Manuel Allenspach

Reputation: 12735

An URI always points to a file, so there is no way to create an URI for a bitmap in the memory.

Like you pointed out, you could attach the image directly to the intent, like this:

 ByteArrayOutputStream bos = new ByteArrayOutputStream();  
 yourBitmap.compress(CompressFormat.PNG, 0, bos);  


 Intent intent = new Intent(); 
 intent.setAction(Intent.ACTION_SEND); 
 intent.setType("*/*"); 
 intent.putExtra(Intent.EXTRA_STREAM, bos.toByteArray());
 startActivity(intent); 

Upvotes: 1

Related Questions