我正在制作绘图应用。现在我想与共享选项菜单共享我的绘图。但是在共享时不会附加绘制画布图像。
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/menu_item_share"
android:showAsAction="ifRoom"
android:title="Share"
android:actionProviderClass=
"android.widget.ShareActionProvider"/>
以下是java文件的代码
public boolean onCreateOptionsMenu(Menu menu) {
/** Inflating the current activity's menu with res/menu/items.xml */
getMenuInflater().inflate(R.menu.share_menu, menu);
/** Getting the actionprovider associated with the menu item whose id is share */
mShareActionProvider = (ShareActionProvider) menu.findItem(R.id.menu_item_share).getActionProvider();
/** Setting a share intent */
mShareActionProvider.setShareIntent(getDefaultShareIntent());
return super.onCreateOptionsMenu(menu);
}
private Intent getDefaultShareIntent(){
Bitmap bitmap = drawView.getDrawingCache();
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + UUID.randomUUID().toString()+".png";
File file = new File(path+"/image.png");
System.out.println("path="+path);
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse(path);
//Uri screenshotUri = Uri.fromFile(file);
sharingIntent.setType("image/png");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
return sharingIntent;
}
请为我提供与不同应用共享画布图像的方法。谢谢
答案 0 :(得分:1)
我知道这是一个迟到的回复。但这可能对某人有所帮助。这是适合我的代码。
private Intent getDefaultShareIntent(){
// Save image to external storage before sending. With out saving, I to got a blank screen as attachment.
String imagePath=saveToExternalSorage(mBitmap);
File f=new File(imagePath);
Uri screenshotUri = Uri.fromFile(f);
//Create an intent to send any type of image
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
return sharingIntent;
}
,其中
public String saveToExternalSorage(Bitmap b){
// Get path to External Storage
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/"+IMAGE_PATH);
myDir.mkdirs();
// Generating a random number to save as image name
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String timeStamp = new SimpleDateFormat("yyyyMMdd").format(new Date());
String fname = "Image_"+timeStamp+"_"+ n + ".jpg";
File file = new File(myDir, fname);
if (file.exists()) {
file.delete();
}
try {
FileOutputStream out = new FileOutputStream(file);
b.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
// Tell your media scanner to refresh/scan for new images
MediaScannerConnection.scanFile(this,
new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
System.out.println(path);
}
});
return file.getAbsolutePath();
}
我想知道,以前保存图像是正确分享的唯一方法。