gpt4 book ai didi

android - Bitmap.createBitmap 返回 null

转载 作者:行者123 更新时间:2023-11-30 02:39:32 25 4
gpt4 key购买 nike

<分区>

如果我在按钮点击监听器中调用下面的方法,我的代码就可以工作。但是当我尝试直接从 onCreate 或 button.performClick() 调用此代码时

bitmap = Bitmap.createBitmap(v1.getDrawingCache());

这一行返回空指针异常。

public void takeScreenShot(){
try{

DateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss");
Date date = new Date();
String d = dateFormat.format(date);
String mPath = Environment.getExternalStorageDirectory().toString() + "/ScreenShot/" + d + ".jpg";
String directory = Environment.getExternalStorageDirectory().toString() + "/ScreenShot/";
File imageFile2 = new File(directory);
if(!imageFile2.isDirectory()){
imageFile2.mkdirs();
}
File imageFile;
// create bitmap screen capture
Bitmap bitmap;
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

OutputStream fout = null;
imageFile = new File(mPath);


try {
fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
fout.flush();
fout.close();

} catch (FileNotFoundException e) {
Log.e("MAYDAY","hata11 " + e.toString());
e.printStackTrace();
} catch (IOException e) {
Log.e("MAYDAY","hata22 " + e.toString());
e.printStackTrace();
}
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, d + ".jpg");
values.put(Images.Media.DESCRIPTION, d + ".jpg");
values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis ());
values.put(Images.ImageColumns.BUCKET_ID, d + ".jpg");
values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, d + ".jpg");
values.put("_data", imageFile.getAbsolutePath());

ContentResolver cr = getContentResolver();
cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}catch(Exception e){
Log.e("MAYDAY","hata33 " + e.toString());
}

}

My LogCat

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com