Incorporer une image dans le corps du courrier électronique de la sauvegarde et de la sélection dans SDCard dans Android

StackOverflow https://stackoverflow.com/questions/19834933

Question

J'essaie d'incorporer une image en ligne dans le corps de messagerie en enregistrant dans SDCard, puis en choisissant de cet endroit, mais l'image n'est pas affichée et juste "OBJ" y est montré. Aidez-moi, voici le code de ma fonction:

   {

    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:"));  }
Était-ce utile?

La solution

Essayez ceci pour enregistrer le fichier:

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

Et assurez-vous d'ajouter la bonne autorisation dans votre manifeste:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top