我对此非常陌生,所以我无法理解很多技术说话......所以,我在一个人的项目中工作,我的目标是当用户按SHARE(顶级菜单)时,它需要一个截图,然后你可以发送它作为附件,它创建文件夹,文件,但每次我通过电子邮件或其他任何方式发送,它附加一个空文件。我从这里选择了例子来做这件事。在清单我已经添加了所有权限(write_external / internal,internet,camera,read_external)
分享课程:
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;
}
}
的活动:
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"));
}
}
答案 0 :(得分:0)
假设文件已保存(您可以使用文件浏览器进行检查),共享jpeg的正确方法是使用jpeg mime类型。所以你应该更换" application / image"你的意图类型" image / jpeg"。看看这个example
答案 1 :(得分:0)
您需要更改此内容;
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()));