来自Gridview的Android意图共享

来自Gridview的Android意图共享,android,gridview,android-intent,share,Android,Gridview,Android Intent,Share,创建一个应用程序,在这个应用程序中,我将一堆照片存储在drawable文件夹中,当用户在照片上长按打开菜单时,其中一个选项是共享。我可以打开contextmenu,但不知道如何从grid菜单共享 我也有扩展它是在自己的看法,并可以从那里分享单一的图像 但是当用户从gridview中选择图像时,我不知道如何共享该图像。 这是当用户选择股票期权时我调用的方法 public Intent intentShare() { Intent share = new Intent(Int

创建一个应用程序,在这个应用程序中,我将一堆照片存储在drawable文件夹中,当用户在照片上长按打开菜单时,其中一个选项是共享。我可以打开contextmenu,但不知道如何从grid菜单共享

我也有扩展它是在自己的看法,并可以从那里分享单一的图像

但是当用户从gridview中选择图像时,我不知道如何共享该图像。

这是当用户选择股票期权时我调用的方法

public Intent intentShare() {    
        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("image/jpeg");
        share.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://your.package.here/drawable/"+share.putExtra("id", lastPosition)));
        startActivity(Intent.createChooser(share, "Share Image"));
        return share;
}
我在其他几个问题上看到了这种可能的选择/解决方案

Bitmap icon = mBitmap;
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
try {
    f.createNewFile();
    FileOutputStream fo = new FileOutputStream(f);
    fo.write(bytes.toByteArray());
} catch (IOException e) {                       
        e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));
但我不知道如何初始化mBitmap,如果它起作用的话


整个代码可以在这里找到:

您可以使用如下代码从资源中转换
位图

Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), mThumbIds[lastPosition]);
现在,您可以在展开视图中共享它