Java 调用菜单上的函数单击Android
我是一名android初学者,正在尝试为溢出菜单实现一个功能。 我的菜单代码是这样的Java 调用菜单上的函数单击Android,java,android,Java,Android,我是一名android初学者,正在尝试为溢出菜单实现一个功能。 我的菜单代码是这样的 public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if(id==android.R.id.home) { finish(); }else if (id == R.id.image) { takeScreenshot(); // getting
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id==android.R.id.home) {
finish();
}else if (id == R.id.image) {
takeScreenshot(); // getting error like The method takeScreenshot(Context, View) in the type QuoteViewActivity is not applicable for the arguments ()
return true;
}
及
函数代码如下
public static void takeScreenshot(Context context, View view) {
String path = Environment.getExternalStorageDirectory().toString()
+ "/" + "test.png";
View v = view.findViewById(android.R.id.content).getRootView();
v.setDrawingCacheEnabled(true);
v.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false);
OutputStream out = null;
File imageFile = new File(path);
try {
out = new FileOutputStream(imageFile);
// choose JPEG format
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
} catch (FileNotFoundException e) {
// manage exception
} catch (IOException e) {
// manage exception
} finally {
try {
if (out != null) {
out.close();
}
} catch (Exception exc) {
}
}
// onPauseVideo();
Intent share = new Intent(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(imageFile));
share.setType("image/png");
((Activity) context).startActivityForResult(
Intent.createChooser(share, "Share Drawing"), 111);
}
我怎样才能在菜单点击中调用它?
很抱歉为开发者提出了一个愚蠢的问题
谢谢处理菜单项点击的方式如下: 首先,设置选项菜单:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater mnuInflater = getSupportMenuInflater();
mnuInflater.inflate(R.menu.your_menu_xml, menu);
return true;
}
在此处处理单击:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.menu_settings:
// EITHER CALL THE METHOD HERE OR DO THE FUNCTION DIRECTLY
yourMethod();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
然后在yourMethod()中执行以下函数:
据我所见,应该是:
takeScreenshot(this, findViewById(R.id.your_root_layout));
但是,您可以调用takeScreenshot()如果takeScreenshot
不是静态的并且在您的活动中,则代码>将被删除
private void takeScreenshot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
openScreenshot(imageFile);
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
}
别忘了这一点
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
这是以什么方式不起作用的?您好@TimCastelijns,我在菜单中遇到了错误,比如类型QuoteViewActivity中的方法takeScreenshot(上下文,视图)不适用于参数()@Rajubai:当所选菜单项id为“R.id.image”时,您正在从OnOptions ItemSelected()调用takeScreenshot()方法。那么你真正想要的是什么呢?你认为你为什么会得到那个错误?Post error STACKTRACE我也做了同样的事情,但是得到了类似“类型QuoteViewActivity中的方法takeScreenshot(Context,View)不适用于参数()”的错误。谢谢如果你想要截图:我已经发布了另一个答案+是!我想用JPEG压缩我的视图并分享它查看我的另一个问题,我已经发布了查看-private void openScreenshot(File imageFile){Intent Intent Intent=new Intent();Intent.setAction(Intent.ACTION_视图);Uri Uri Uri=Uri.fromFile(imageFile);Intent.setDataAndType(Uri,“image/*”);startActivity(Intent);}
Hi!takeScreenshot和takeScreenshot一样不在任何版面中声明static@RajubhaiRathod假设您想在MainActivity
中调用此函数。然后,这个
是MainActivity
和R.id的实例。你的“根”布局
是MainActivity
的根布局。我不需要拍摄全屏截图,但我需要一个名为@quoteCard的视图,从我的布局中,我还希望在创建它之后共享意图。请给我密码。谢谢看这段代码会截取全屏,对吗?我只想捕获一个名为@quoteCardit的CardView并不容易,但我没有找到任何其他内容
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>