我创建了一个函数,将截图保存在.jpeg类型的临时文件中,然后允许用户在facebook或蓝牙上共享。以下是我的分享功能:
public Bitmap Share(View v) {
// Sound
soundPool.play(button_sound, 1.0f, 1.0f, 0, 0, 1.0f);
// Image
v.setDrawingCacheEnabled(true);
v.setLayerType(View.LAYER_TYPE_NONE, null);
Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + "temporary_file.jpg");
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
// Share
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
String filel = "file://" + Environment.getExternalStorageDirectory()
+ File.separator + "temporary_file.jpg";
share.putExtra(Intent.EXTRA_STREAM, Uri.parse(filel));
startActivity(Intent.createChooser(share, "Share Image"));
return bitmap;
}我的问题是,它需要截图,但当我尝试分享一个新的截图时,总是反复分享相同的截图。当我使用文件管理器检查时,图像是不同的。所以我不知道是什么原因造成的。
非常感谢你抽出时间。
发布于 2014-05-21 09:40:06
首先,只有当文件不存在时,file.createNewFile()才能工作。它不会在失败时抛出任何异常,它只返回true表示成功,返回false表示失败,
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, ostream);也是如此。此方法也不会在失败时抛出任何异常,只返回true表示成功,返回false表示失败,
我不知道它是否导致了错误,但您可能需要研究一下。例如,当文件已经存在时,您可以尝试先删除它。
发布于 2014-05-21 09:12:39
请在getBitmap之前使您的视图无效
看上去:
v.inValidated();
v.setDrawingCacheEnabled(true);https://stackoverflow.com/questions/23777687
复制相似问题