【问题标题】:How to attach image from drawable to gmail?如何将图像从drawable附加到gmail?
【发布时间】:2015-08-06 04:51:27
【问题描述】:

我正在尝试将我的 gridview 中的图像附加到 gmail 或 facebook,但是每当我尝试附加我的应用程序时都会崩溃,并且我收到以下错误并出现空指针异常,以下是我的 gridview 图像选择代码,任何人都可以帮忙?

public class Free_Cover_Activity extends AppCompatActivity
{
 GridView grid;
 int[] imageId = {
  R.drawable.discovercover,
  R.drawable.burpresswordfree,
  R.drawable.flyfree,
  R.drawable.cantmovefree,
  R.drawable.cantmovewordfree,
  R.drawable.chalkthisfree,
  R.drawable.fivehundredmetersfree,
  R.drawable.freeexercise,
  R.drawable.gym_smilie,
  R.drawable.hundredcalrairesfree,
  R.drawable.injuryfree,
  R.drawable.jumpropefree,
  R.drawable.nicesnathcfree,
  R.drawable.personglrecordfree,
  R.drawable.posefree,
  R.drawable.pushupfree,
  R.drawable.shoulder,
  R.drawable.timewordfree,
  R.drawable.unbrokernfree,
  R.drawable.weightbeltfree
 };

 private Bitmap mBitmap;
 private Intent email;

 @Override
 protected void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);

  setContentView(R.layout.free_cover_gridview);
  android.support.v7.app.ActionBar abar =  getSupportActionBar();
  ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#D64365"));
  abar.setBackgroundDrawable(colorDrawable);
  abar.hide();

  CustomGridFreeCover adapter = 
               new CustomGridFreeCover(Free_Cover_Activity.this, imageId);
  grid=(GridView)findViewById(R.id.freecover_grid);
  grid.setAdapter(adapter);
  grid.setOnItemClickListener(new AdapterView.OnItemClickListener()
  {
   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id)
   {
    try
    {
     Bitmap largeIcon = 
         BitmapFactory.decodeResource(getResources(), R.drawable.discovercover);

     /*
     replace "R.drawable.bubble_green" with the image resource 
     you want to share from drawable
     */
     ByteArrayOutputStream bytes = new ByteArrayOutputStream();
     largeIcon.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

     //you can create a new file name "test.jpg" in sdcard folder.
     File f = 
        new File(Environment.getExternalStorageDirectory() + File.separator + "test.jpg");
     f.createNewFile();

     //write the bytes in file
     FileOutputStream fo = new FileOutputStream(f);
     fo.write(bytes.toByteArray());

     //remember close de FileOutput
     fo.close();
    } catch (IOException e) {
     //TODO Auto-generated catch block
     e.printStackTrace();
    }

    Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
    shareIntent.setType("image/jpeg");
    //set your subject
    shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Hi"); 
    //set your message
    shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "How are you"); 

    String imagePath = Environment.getExternalStorageDirectory() 
           + File.separator + "test.jpg";
    File imageFileToShare = new File(imagePath);
    Uri uri = Uri.fromFile(imageFileToShare);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(shareIntent, "Share Image"));
   }
  });
 }

 public class CustomGridFreeCover extends BaseAdapter
 {
  private Context mContext;
  //private final String[] web;
  private final int[] Imageid;

  public CustomGridFreeCover(Context c,int[] Imageid )
  {
   mContext = c;
   this.Imageid = Imageid;
   //this.web = web;
  }

  @Override
  public int getCount()
  {
   //TODO Auto-generated method stub
   return Imageid.length;
  }

  @Override
  public Object getItem(int position) {
   //TODO Auto-generated method stub
   return null;
  }

  @Override
  public long getItemId(int position) {
   //TODO Auto-generated method stub
   return 0;
  }

  @Override
  public View getView(int position, View convertView, ViewGroup parent)
  {
   //TODO Auto-generated method stub
   View grid;
   LayoutInflater inflater = (LayoutInflater) mContext
    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    if (convertView == null)
    {
     grid = new View(mContext);
     grid = inflater.inflate(R.layout.free_cover_griditem, null);
     //TextView textView = (TextView) grid.findViewById(R.id.grid_text);
     ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image_freecover);
     //textView.setText(web[position]);
     imageView.setImageResource(Imageid[position]);
    } else {
     grid = (View) convertView;
    }
   return grid;
  }
 }
}

【问题讨论】:

  • 图像存储在哪里?是在资源还是文件系统上?
  • 我的drawable中的图片
  • 罗马的事情是你传递一个空位图对象 Bitmap icon = mBitmap;您需要分配位图,然后代码将开始工作
  • 我粘贴为答案

标签: android android-intent bitmap gmail


【解决方案1】:

这是您需要的工作代码:

首先将图片从Drawable保存到SD Card这里是代码:

try{

Bitmap largeIcon = BitmapFactory.decodeResource(getResources(), R.drawable.bubble_green);

            //replace "R.drawable.bubble_green" with the image resource you want to share from drawable 

            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            largeIcon.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

            // you can create a new file name "test.jpg" in sdcard folder.
            File f = new File(Environment.getExternalStorageDirectory() + File.separator + "test.jpg");

            f.createNewFile();

            // write the bytes in file
            FileOutputStream fo = new FileOutputStream(f);
            fo.write(bytes.toByteArray());

            // remember close de FileOutput
            fo.close();
        }
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

然后从SD card 获取保存的图像并附加在电子邮件意图中,如下所示:

        Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
        shareIntent.setType("image/jpeg");

        shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Hi"); //set your subject
        shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "How are you"); //set your message

        String imagePath = Environment.getExternalStorageDirectory() + File.separator + "test.jpg";

        File imageFileToShare = new File(imagePath);

        Uri uri = Uri.fromFile(imageFileToShare);

        shareIntent.putExtra(Intent.EXTRA_STREAM, uri);

        startActivity(Intent.createChooser(shareIntent, "Share Image"));

【讨论】:

  • f.createNewFile();..red line unhandled exception
  • 问题是图像已附加,但无法查看且无法发送..我正在设备 samsung edge.lollipop 版本中进行测试
  • @Roman 我试过这段代码,它在 Nexus 9 (Lollipop) 中运行良好。您是否在清单中设置了&lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt; 权限?
  • @Roman 好的,检查您的目录是否可以看到“test.jpg”?尝试在其他设备上运行相同的代码,检查它是否在那里工作?
【解决方案2】:

这对我来说非常有效。

            Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
            shareIntent.setType("image/jpeg");

            shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, getResources().getString(R.string.share_subject));
            shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, getResources().getString(R.string.share_message));

            shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://" + C.PROJECT_PATH + "/drawable/" + R.drawable.image_to_share);

            startActivity(Intent.createChooser(shareIntent, "Share Image"));

但我始终建议从此资源中解码 Bitmap,将其保存为存储中的 File,然后使用该文件。直接使用资源的更可靠的方法。

【讨论】:

  • 这里的应用是什么??
  • 我在我的应用程序中使用了 Context 作为变量,你可以使用 getResources()。我已经更新了答案,请检查一下。
  • 如果我有所有的图片 png
  • 我尝试了你的代码..图像名称丢失并且扩展名丢失
  • 改为shareIntent.setType("image/png");
【解决方案3】:

罗马的事情是你正在传递一个空位图对象

 Bitmap icon = mBitmap;

您需要先分配位图,然后代码才会开始工作

有关更多信息,您可以查看以下链接:-

https://github.com/codepath/android_guides/wiki/Sharing-Content-with-Intents

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

【讨论】:

  • 链接扩展未发送到 gmail 或 fb
  • hello roman,您需要先将可绘制或图像源作为 URI 传递,然后您可以使用以下意图发送该 URI:- Intent shareIntent = new Intent() ; shareIntent.setAction(Intent.ACTION_SEND); shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage); shareIntent.setType("图片/jpeg"); startActivity(Intent.createChooser(shareIntent,"分享图片"));请注意 uriToImage 是将您的图像资源存储为 uri 的对象
  • 请看我编辑的答案我已经更新了链接你可以找到一个信息github.com/codepath/android_guides/wiki/…
  • icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);..根据错误行它在这里显示问题
  • 对象“icon”为空表示其中没有图像位图。所以你需要将位图或图像作为位图分配给对象“icon”
【解决方案4】:

首先将你的 drwable 转换为图像并将其存储在 SDCard 上

Store Drawable to SD card


然后

private void shareImage() {
        Intent share = new Intent(Intent.ACTION_SEND);

        // setType("image/png"); OR for jpeg: setType("image/jpeg");
        share.setType("image/*");

        // Make sure you put example png image named yourImg.png in your
        // directory

        String imagePath = Environment.getExternalStorageDirectory()
                + "/yourImg.png";

        File imageFileToShare = new File(imagePath);

        Uri uri = Uri.fromFile(imageFileToShare);
        share.putExtra(Intent.EXTRA_STREAM, uri);

        startActivity(Intent.createChooser(share, "Share Image!"));
    }

【讨论】:

  • 图片正在附加..但无法发送
  • 网格的点击监听有什么问题吗?
  • 没有,除非您加载正确的图像。您是否检查过图像是否显示在 SD 卡上?
  • 我在drawable中有图片
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-11
  • 1970-01-01
  • 2023-01-23
  • 1970-01-01
  • 1970-01-01
  • 2016-03-02
相关资源
最近更新 更多