首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在android中使用“分享图片”分享意图来分享图片?

如何在android中使用“分享图片”分享意图来分享图片?
EN

Stack Overflow用户
提问于 2011-10-05 21:17:13
回答 18查看 211.2K关注 0票数 86

我有一个图片画廊应用程序,我把所有的图片都放到了drawable-hdpi文件夹中。在我的活动中,我将图像命名为:

代码语言:javascript
运行
复制
private Integer[] imageIDs = {
        R.drawable.wall1, R.drawable.wall2,
        R.drawable.wall3, R.drawable.wall4,
        R.drawable.wall5, R.drawable.wall6,
        R.drawable.wall7, R.drawable.wall8,
        R.drawable.wall9, R.drawable.wall10
};

所以现在我想知道如何使用共享意图共享这些图像,我放入的共享代码如下:

代码语言:javascript
运行
复制
     Button shareButton = (Button) findViewById(R.id.share_button);
     shareButton.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {
       
        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        Uri screenshotUri = Uri.parse(Images.Media.EXTERNAL_CONTENT_URI + "/" + imageIDs);

        sharingIntent.setType("image/jpeg");
        sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
        startActivity(Intent.createChooser(sharingIntent, "Share image using"));  
    
         }
    });

我也有共享按钮,当我点击共享按钮时,共享框是打开的,但当我剪切任何服务时,它大多崩溃或一些服务说:无法打开图像,所以我如何解决这个问题,或者有任何其他格式的代码来共享图像?

编辑:

我尝试使用下面的代码。但它不起作用。

代码语言:javascript
运行
复制
Button shareButton = (Button) findViewById(R.id.share_button);
     shareButton.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {

        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        Uri screenshotUri = Uri.parse("android.resource://com.android.test/*");
        try {
            InputStream stream = getContentResolver().openInputStream(screenshotUri);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        sharingIntent.setType("image/jpeg");
        sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
        startActivity(Intent.createChooser(sharingIntent, "Share image using"));  

         }
    });

如果不介意,请改正我上面的代码,或者给我一个合适的例子,请告诉我如何从drawable-hdpi文件夹中分享我的图片

EN

回答 18

Stack Overflow用户

发布于 2011-10-05 21:38:04

代码语言:javascript
运行
复制
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"));
票数 118
EN

Stack Overflow用户

发布于 2013-10-27 21:13:07

superM提出的解决方案对我来说工作了很长一段时间,但最近我在4.2 (HTC )上测试了它,它在那里停止了工作。我知道这是一种变通方法,但它是唯一适用于我所有设备和版本的变通方法。

根据文档,开发人员被要求使用"use the system MediaStore"发送二进制内容。然而,这具有媒体内容将被永久保存在设备上的(不)优点。

如果这是您的选择,您可能希望授予权限WRITE_EXTERNAL_STORAGE并使用系统范围的MediaStore。

代码语言:javascript
运行
复制
Bitmap icon = mBitmap;
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");

ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "title");
values.put(Images.Media.MIME_TYPE, "image/jpeg");
Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,
        values);


OutputStream outstream;
try {
    outstream = getContentResolver().openOutputStream(uri);
    icon.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
    outstream.close();
} catch (Exception e) {
    System.err.println(e.toString());
}

share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image"));
票数 43
EN

Stack Overflow用户

发布于 2015-05-07 17:21:00

第一次添加权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

使用资源中的位图

代码语言:javascript
运行
复制
Bitmap b =BitmapFactory.decodeResource(getResources(),R.drawable.userimage);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(getContentResolver(), b, "Title", null);
Uri imageUri =  Uri.parse(path);
share.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(share, "Select"));

通过蓝牙和其他信使进行测试

票数 28
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7661875

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档