在我的应用中,我将webview的文本保存为图像文件并启动共享功能以允许用户共享文件,图像也保存在图片文件夹中。我可以确认它们确实通过文件浏览器出现在图片文件夹中,但Gallery没有更新。
我用来拍摄图像的代码是
private void htmlCapture2() {
if (!checksd()) {
Log.e("sdcard", "failed sd card check");
//we dont have permision so lets not continue
return;
}
if (!checkFolder()) {
Log.e("folder", "failed folder check");
//we dont have a folder so lets not continue
return;
}
WebView webView = (WebView) findViewById(R.id.webView);
webView.setDrawingCacheEnabled(true);
webView.buildDrawingCache();
Bitmap bm = screenshot(webView);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE,"title");
values.put(MediaStore.Images.Media.MIME_TYPE,"image/png");
Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
OutputStream outstream;
try {
outstream = getContentResolver().openOutputStream(uri);
bm.compress(Bitmap.CompressFormat.PNG,100,outstream);
outstream.close();
} catch (Exception e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM,uri);
share.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1);
startActivity(Intent.createChooser(share,"Share Image"));
}
public static Bitmap screenshot(WebView webView) {
try {
//float scale = webView.getScaleX();
int height;
height = webView.getHeight();
Bitmap bitmap = Bitmap.createBitmap(webView.getWidth(), height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(0xffffffff);
webView.draw(canvas);
return bitmap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
我花了三天时间才能做到这一点,甚至更多地寻找最新问题的解决方案 我知道我需要使用MediaScannerConnection,但它需要一个文件名,我没有,因为我没有真正创建一个文件。 我已经看过扫描SD卡的解决方案,但这似乎是一个坏主意,当我试图摆脱绝望时,他们似乎无法使用最新的SDK
任何帮助非常感谢
答案 0 :(得分:0)
您需要通知Android MediaStore已添加新图像,您可以通过以下方式执行此操作
public Uri addImageToMediaStore(ContentResolver mresolver, String imgType, File filepath) {
ContentValues Cvalues = new ContentValues();
Cvalues.put(MediaStore.Images.Media.TITLE, "player");
Cvalues.put(MediaStore.Images.Media.DISPLAY_NAME, "player");
Cvalues.put(MediaStore.Images.Media.DESCRIPTION, "");
Cvalues.put(MediaStore.Images.Media.MIME_TYPE, "image/" + imgType);
Cvalues.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
Cvalues.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
Cvalues.put(MediaStore.Images.Media.DATA, filepath.toString());
return mresolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Cvalues);
}
希望这有帮助
答案 1 :(得分:0)
根据CommonsWare的建议,我采用了文件保存方法(我知道它不是推荐的方法,但确实有工作的优点)
private void htmlCapture2() {
if (!checksd()) {
Log.e("sdcard", "failed sd card check");
//we dont have permision so lets not continue
return;
}
if (!checkFolder()) {
Log.e("folder", "failed folder check");
//we dont have a folder so lets not continue
return;
}
WebView webView = (WebView) findViewById(R.id.webView);
webView.setDrawingCacheEnabled(true);
webView.buildDrawingCache();
Bitmap bm = screenshot(webView);
String filename = "capture_" + z.getCurrentTimeStamp() + ".png";
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "", filename);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/png");
//ContentValues values = new ContentValues();
//values.put(MediaStore.Images.Media.TITLE,"title");
//values.put(MediaStore.Images.Media.MIME_TYPE,"image/png");
//Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
OutputStream outstream;
try {
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, ostream);
Log.i("ExternalStorage","I think the path is : "+file.toString());
ostream.close();
Log.i("ExternalStorage","I think the path is : "+file.toString());
MediaScannerConnection.scanFile(this,
new String[] { file.toString() }, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
//outstream = getContentResolver().openOutputStream(uri);
//bm.compress(Bitmap.CompressFormat.PNG,100,outstream);
//outstream.close();
} catch (Exception e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
//share.putExtra(Intent.EXTRA_STREAM,uri);
share.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,1);
startActivity(Intent.createChooser(share,"Share Image"));
}
我当然要做一些整理,图片并没有出现在画廊中,但是如果你去画廊中下载专辑,那就是以前的改进。