Question

I'm trying to embed a inline image in email body by saving in sdcard and then picking from that place but image is not being shown and just "obj" is shown there.Kindly help me out,here is the code of my function:

   {

    Bitmap newImg=BitmapFactory.decodeByteArray(img,0,img.length);

    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/saved_images");    
    myDir.mkdirs();
    String fname = "stoneage.jpg";
    File file = new File (myDir, fname);

    try{
           FileOutputStream out = new FileOutputStream(file);
           newImg.compress(Bitmap.CompressFormat.JPEG, 90, out);
           out.flush();
           out.close();

    } catch (Exception e) {
           e.printStackTrace();
    }

    Uri uri = null;
    uri = Uri.parse("file://" + Environment.getExternalStorageDirectory()+"/saved_images/"+fname);


    String txtBody = "<html><body><h1>hi it is stoneage product</h1><br><img src ="+uri+"/></body></html>";
    Log.d("data", txtBody);

    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("text/html");

  emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "testemail");
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(txtBody));
    startActivity(Intent.createChooser(emailIntent, "Email:"));  }
Was it helpful?

Solution

Try this to save the file:

    try {
        File myFile = new File("/sdcard/mysdfile.jpg");
        myFile.createNewFile();
        FileOutputStream fOut = new FileOutputStream(myFile);
        OutputStreamWriter myOutWriter = 
                                new OutputStreamWriter(fOut);
        myOutWriter.append(txtData.getText());
        myOutWriter.close();
        fOut.close();
        Toast.makeText(getBaseContext(),
                "Done writing SD 'mysdfile.txt'",
                Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        Toast.makeText(getBaseContext(), e.getMessage(),
                Toast.LENGTH_SHORT).show();
    }





    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
    emailIntent.setType("application/image");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{strEmail}); 
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Test Subject"); 
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "From My App"); 
    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///mnt/sdcard/mysdfile.jpg"));
    startActivity(Intent.createChooser(emailIntent, "Send mail..."));

And make sure you add the correct permission in your manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top