我正在尝试通过保存SDCARD并从该位置进行选择,但没有显示图像,但在那里显示“ OBJ”。

   {

    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:"));  }
有帮助吗?

解决方案

尝试此操作以保存文件:

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

并确保在清单中添加正确的权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top