【问题标题】:how to format the path to use in uri.parse() for android如何格式化要在 uri.parse() for android 中使用的路径
【发布时间】:2011-12-29 03:57:45
【问题描述】:

我的图片位于以下位置(在我的 android 工作区资源中),

D:\Android\WorkSpace\myprojectname\res\drawable-hdpi

我已使用以下代码行将此图像附加到电子邮件,但它似乎不起作用,它发送电子邮件但它没有附件。

emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,Uri.parse("android.resource://com.mywebsite.myprojectname/" + R.drawable.image));

这是错的吗?

【问题讨论】:

  • 您是否有机会使用 Netbeans?

标签: android parsing path location uri


【解决方案1】:

其他答案也可能有用(但它们对我不起作用)!我只使用下面的一行代码就让它运行起来了,这非常简单易行。感谢大家。

emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.image));

我认为我的问题是我把包名弄错了,但是使用 getPackageName() 方法问题解决了!

【讨论】:

    【解决方案2】:
        Resources res = this.getResources();
        Bitmap bm = BitmapFactory.decodeResource(res, R.drawable.image);
    
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    
        File f = new File(Environment.getExternalStorageDirectory(), "image.jpg");
    
        try {
            f.createNewFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //write the bytes in file
        FileOutputStream fo = null;
        try {
            fo = new FileOutputStream(f);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            fo.write(bytes.toByteArray());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Uri uri = Uri.fromFile(f);
    

    之后,

    您在哪里发送电子邮件,请执行以下操作,

        intent.putExtra(Intent.EXTRA_STREAM, uri);
    

    它肯定会起作用。它完全有效。试试看。

    【讨论】:

      【解决方案3】:

      嗯,第一个问题是,即使该 URI 格式正确(我对此表示怀疑),该文件也会在您的应用程序的沙箱中,电子邮件活动(或任何其他活动,为此)无法访问该沙箱事情)。在任何情况下,您都必须将该文件写入 SD 卡并让电子邮件程序从那里读取它。您可以使用以下代码输出位图:

      Bitmap image = BitmapFactory.decodeResource(getResources(),R.drawable.image);
      File file = new File(Environment.getExternalStorageDirectory(), "forEmail.PNG");
      
      OutputStream outStream = new FileOutputStream(file);
      image.compress(Bitmap.CompressFormat.PNG, 100, outStream);
      outStream.flush();
      outStream.close();
      

      然后使用Uri.fromFile()从上面定义的文件中生成URI:

      emailIntent.putExtra(android.content.Intent.EXTRA_STREAM, Uri.fromFile(file));
      

      (资源到文件的代码改编自here

      【讨论】:

      • 在 android 电子邮件页面上显示它已附加该图像,但在接收方端没有电子邮件附件!
      猜你喜欢
      • 2011-12-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-08
      相关资源
      最近更新 更多