Android Eclipse:在没有mediaStore.Insert()的情况下将图像共享到Facebook

时间:2014-04-06 19:47:19

标签: android eclipse facebook android-intent bitmap

我想使用Intent分享截图到Facebook,Twitter等。我找到了通过将图像插入媒体存储区然后抓取此图像的URI来实现此目的的代码示例。但是,我不想用这些无用的图像阻塞用户的设备,我也不想要求访问外部权限,因为我不在应用程序的任何其他位置使用它。有没有办法构建意图,以便它可以共享图像,而无需拥有URI?比如直接传递位图,或者在压缩到.png之后传递它?或者有没有办法为内存中保存的位图构建URI,而不首先将其作为文件?

我现在正在使用它:

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

1 个答案:

答案 0 :(得分:1)

URI始终指向文件,因此无法为内存中的位图创建URI。

正如您指出的那样,您可以将图像直接附加到意图上,如下所示:

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