我需要获取View
的屏幕截图。我尝试了两种方法来完成这项工作。不幸的是,两者都导致同一个错误。
这是日志:
java.lang.IllegalArgumentException: Software rendering doesn't support hardware bitmaps
at android.graphics.BaseCanvas.null onHwBitmapInSwMode(null)(BaseCanvas.java:550)
at android.graphics.BaseCanvas.null throwIfHwBitmapInSwMode(null)(BaseCanvas.java:557)
at android.graphics.BaseCanvas.null throwIfCannotDraw(null)(BaseCanvas.java:69)
at android.graphics.BaseCanvas.null drawBitmap(null)(BaseCanvas.java:127)
at android.graphics.Canvas.null drawBitmap(null)(Canvas.java:1504)
at android.graphics.drawable.BitmapDrawable.null draw(null)(BitmapDrawable.java:545)
at android.widget.ImageView.null onDraw(null)(ImageView.java:1355)
at android.view.View.null draw(null)(View.java:20248)
at android.view.View.null draw(null)(View.java:20118)
at android.view.ViewGroup.null drawChild(null)(ViewGroup.java:4336)
at android.view.ViewGroup.null dispatchDraw(null)(ViewGroup.java:4115)
at android.view.ViewOverlay$OverlayViewGroup.null dispatchDraw(null)(ViewOverlay.java:251)
at android.view.View.null draw(null)(View.java:20251)
at android.view.View.null buildDrawingCacheImpl(null)(View.java:19516)
at android.view.View.null buildDrawingCache(null)(View.java:19379)
at android.view.View.null getDrawingCache(null)(View.java:19215)
at android.view.View.null getDrawingCache(null)(View.java:19166)
at com.omnipotent.free.videodownloader.pro.utils.ViewUtils.android.graphics.Bitmap captureView(android.view.View)(ViewUtils.java:70)
at com.omnipotent.free.videodownloader.pro.ui.main.MainActivity.com.omnipotent.free.videodownloader.pro.data.bean.TabBean getCurrentTabsData()(MainActivity.java:325)
at com.omnipotent.free.videodownloader.pro.ui.main.MainActivity.com.omnipotent.free.videodownloader.pro.data.bean.TabBean access$getCurrentTabsData(com.omnipotent.free.videodownloader.pro.ui.main.MainActivity)(MainActivity.java:84)
at com.omnipotent.free.videodownloader.pro.ui.main.MainActivity$onAddTab$1.void run()(MainActivity.java:628)
at android.os.Handler.null handleCallback(null)(Handler.java:873)
at android.os.Handler.null dispatchMessage(null)(Handler.java:99)
at android.os.Looper.null loop(null)(Looper.java:193)
at android.app.ActivityThread.null main(null)(ActivityThread.java:6936)
at java.lang.reflect.Method.null invoke(null)(Method.java)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.null run(null)(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.null main(null)(ZygoteInit.java:870)
我仔细检查了代码,并在Internet上查找了相关文章。但是,我尚未解决,这确实让我感到沮丧。 此错误仅在android O以上发生过。
这是我尝试过的两种方法:
方法1:
public static Bitmap captureView(View view) {
Bitmap tBitmap = Bitmap.createBitmap(
view.getWidth(), view.getHeight(), Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(tBitmap);
view.draw(canvas);
canvas.setBitmap(null);
return tBitmap;
}
方法2:
public static Bitmap captureView(View view) {
if (view == null) return null;
boolean drawingCacheEnabled = view.isDrawingCacheEnabled();
boolean willNotCacheDrawing = view.willNotCacheDrawing();
view.setDrawingCacheEnabled(true);
view.setWillNotCacheDrawing(false);
final Bitmap drawingCache = view.getDrawingCache();
Bitmap bitmap;
if (null == drawingCache) {
view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.buildDrawingCache();
bitmap = Bitmap.createBitmap(view.getDrawingCache());
} else {
bitmap = Bitmap.createBitmap(drawingCache);
}
view.destroyDrawingCache();
view.setWillNotCacheDrawing(willNotCacheDrawing);
view.setDrawingCacheEnabled(drawingCacheEnabled);
return bitmap;
}
需要提及的是,我为android:hardwareAccelerated="true"
设置了Activity
,并在其中调用了captureView
方法。
答案 0 :(得分:3)
您可以尝试将硬件类型位图复制到您想要的类型位图,例如:
// function that takes user's input after a quick validation test
// returned value is the user's input
function query() {
// validation code
const input = document.getElementById('search').value;
if(input.length < 3){
alert("Please enter more than two characters.");
return; // Added this so that we're not returning a value that's too short
}
return input;
}
// function that retrieves data from API endpoint by concatenating API url with query variable
function getData(){
const qParam = query();
if( !qParam ) return; // if there's no valid query value, no need to try to call the API
fetch(`https://github.uconn.edu/api/v3/search/users?q=${qParam}`)
.then((response) => response.json())
.then((response) => {
console.log(response);
})
// catch is a callback function for when request fails
.catch((error) => {
console.log(error);
})
}
searchBtn.addEventListener('click', getData);
答案 1 :(得分:0)
使用它来捕获视图(适用于android 8.0):
class CaptureView{
static Bitmap capture(View view){
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
return view.getDrawingCache(); //Returns a bitmap
}
}
Bitmap bitmap = CaptureView.capture(yourView) // Pass in the view to capture
答案 2 :(得分:0)
阅读Glide hardware bitmap docs,找到from unittest import TestCase
from app.application import create_app, db
class BaseTestCase(TestCase):
def setUp(self):
self.app = create_app()
self.db = db
self.app.config['TESTING'] = True
self.app_context = self.app.app_context()
self.app_context.push()
self.client = self.app.test_client()
def tearDown(self):
self.db.session.remove()
self.db.get_engine(self.app).dispose()
self.app_context.pop()
类,可以解决此错误。
使用PixelCopy
将PixelCopy
转换为Android O以上的view
,并使用Android O以下的先前方法。
这是我的代码:
Bitmap
不足之处是我必须使用回调,我不太喜欢。
希望它能起作用。