我正在使用this回答中的几个命令将我的位图保存在SD卡上,然后通过意图分享。
这是我的最终代码:
View u = findViewById(R.id.mainL);
u.setDrawingCacheEnabled(true);
LinearLayout z = (LinearLayout) findViewById(R.id.mainL);
int totalHeight = z.getHeight();
int totalWidth = z.getWidth();
u.layout(0, 0, totalWidth, totalHeight);
u.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(u.getDrawingCache());
u.setDrawingCacheEnabled(false);
String filePath = Environment.getExternalStorageDirectory()
+ File.separator + "pics/screenshot.jpeg";
File imagePath = new File(filePath);
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
b.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
Uri bmpUri = Uri.parse(filePath);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpeg");
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(Intent.createChooser(shareIntent, "Share"));
但现在我有两个问题。
1)结果Image(screenshot.png)无法通过移动图库访问(虽然在SD卡中的pics文件夹中有图像文件)。
2)当我尝试通过意图分享它时,它不会发送,例如当我通过蓝牙发送时,接收器小工具会中断发送操作。
感谢。
答案 0 :(得分:1)
哦,只需在图库中添加任意图片后粘贴此行,它就会刷新您的图库
它对我有用希望能帮助你:)
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(filePath))));
答案 1 :(得分:1)
试试这个
//save image into sdcard
FrameLayout f=(FrameLayout)findViewById(R.id.framelayout);
f.setDrawingCacheEnabled(true);
Bitmap bm = f.getDrawingCache();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageInByte1 = stream.toByteArray();
String path = Environment.getExternalStorageDirectory().toString();
File imgDirectory = new File(Environment.getExternalStorageDirectory()+"/images/");
imgDirectory.mkdirs();
OutputStream fOut = null;
File file = null;
file = new File(path,"/images/"+etcardname.getText().toString()+ ".png");
Toast.makeText(getBaseContext(), "saved at: " + file.getAbsolutePath(), Toast.LENGTH_LONG).show();
fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
//share image
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file));
startActivity(Intent.createChooser(share, "Share image using"));