它创建文件夹和文件,但从不附加它

It creates the folder and file, but never attach it

我在这方面很新,所以我还不能理解很多技术演讲...所以,我在一个个人项目中工作,我的 objective 是当用户按下共享(顶部菜单),它会截取屏幕截图,然后您可以将其作为附件发送,它会创建文件夹和文件,但每次我通过电子邮件或其他方式发送时,它都会附加一个空文件。我从这里挑选了一些例子来做到这一点。在清单中我已经添加了所有权限(write_external/internal、互联网、相机、read_external)

分享class:

public class ShareScreen {

File picFile;

public void shareit(View view, Context context) {

    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        File picDir = new File(Environment.getExternalStorageDirectory()
                + "/SOS Code");
        if (!picDir.exists()) {
            picDir.mkdir();
        }
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache(true);
        Bitmap bitmap = view.getDrawingCache();
        // Date date = new Date();
        String fileName = "SOScode" + ".jpeg";
        picFile = new File(picDir + "/" + fileName);
        try {
            picFile.createNewFile();
            FileOutputStream picOut = new FileOutputStream(picFile);
            bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                    (int) (bitmap.getHeight() / 1.2));
            boolean saved = bitmap.compress(CompressFormat.JPEG, 100,
                    picOut);
            if (saved) {
                Toast.makeText(
                        getApplicationContext(),
                        "Image saved to your device Pictures "
                                + "directory!", Toast.LENGTH_SHORT).show();
            } else {
                // Error
            }
            picOut.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        view.destroyDrawingCache();
        ((QRActivity) context).callIntent();

    } else {
        // Error

    }

}

public File getPicFile() {
    return picFile;
}

private Context getApplicationContext() {
    // TODO Auto-generated method stub
    return null;
}

private View findViewById(int qrrelative) {
    // TODO Auto-generated method stub
    return null;
 }
}

Activity:

 public class QRActivity extends ActionBarActivity {

View view;
ShareScreen screen;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_qr);
    view = (View) findViewById(R.id.QrRelative);// your layout id
    Typeface tf = Typeface.createFromAsset(getAssets(),"verdanab.ttf");
    TextView titleQR = (TextView) findViewById(R.id.titleQR);
    titleQR.setTypeface(tf);
}

public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.share:
            screen = new ShareScreen();
            screen.shareit(view.getRootView(), QRActivity.this);

            break;
        default:
            break;
    }
    return true;
}

public void callIntent() {
    Intent shareCode = new Intent(Intent.ACTION_SEND);
    shareCode.setType("application/image");
    shareCode.putExtra(Intent.EXTRA_STREAM,
            Uri.parse(screen.getPicFile().getAbsolutePath()));
    shareCode.putExtra(android.content.Intent.EXTRA_SUBJECT, "SOS Code");
    shareCode.putExtra(android.content.Intent.EXTRA_TEXT, "Your SOS Code");

    startActivity(Intent.createChooser(shareCode, "Share via"));
 }

}

假设文件已保存(您可以使用文件资源管理器查看),共享 jpeg 的正确方法是使用 jpeg mime 类型。因此,您应该将 Intent 的 "application/image" 类型替换为 "image/jpeg"。看看这个example

你需要改变这个;

 shareCode.setType("application/image");
shareCode.putExtra(Intent.EXTRA_STREAM,
        Uri.parse(screen.getPicFile().getAbsolutePath()));

shareCode.setType("image/*");
shareCode.putExtra(Intent.EXTRA_STREAM,
        Uri.fromFile(screen.getPicFile()));