简体   繁体   中英

how to format the path to use in uri.parse() for android

I have an image that is in the following location (it's in my android workspace resources),

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

I have used the following line of code to attach this image to an email but it doesn't seem to be working, it sends the email but it won't have the attachment.

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

this this wrong?

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

After that,

Where ur sending the email, do the following,

    intent.putExtra(Intent.EXTRA_STREAM, uri);

It will work for sure. it worked for completely. Try it.

Well, the first problem is that even if that URI format is right (which I doubt), that file would be inside your application's sandbox, which is not accessible to the E-mail activity (or any other activity, for that matter). In any case, you're gonna have to write that file into the SD card and have the email program read it from there. You can output the bitmap using the following code:

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

Then use Uri.fromFile() to generate the URI from the file defined above:

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

(Code for resource to file adapted from here )

The other answers might work too (they didn't work for me though)! I got it running using only the one line of code bellow, which is very simple and easy. Thanks everyone.

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

I think my issue was that I was putting the package name wrong, but using the getPackageName() method that issue is solved!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM