Domanda

Sto cercando di incorporare un'immagine in linea nel corpo e -mail salvando in sdcard e poi raccogliendo da quel luogo, ma l'immagine non viene mostrata e solo "obj" è mostrato lì. Mi aiuta, ecco il codice della mia funzione:

   {

    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:"));  }
È stato utile?

Soluzione

Prova questo per salvare il 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..."));

E assicurati di aggiungere l'autorizzazione corretta nel tuo manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top