0
I want to call a method in Android Studio, after the user selects an option. However, I have tried in various ways through getItemId(), but I call the method and nothing occurs. Is it a problem in the takeScreenshot method, or should I use a new Intent? If so, how could I do this?
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.main_add_sticker) {
Intent intent = new Intent(this, StickerSelectActivity.class);
startActivityForResult(intent, SELECT_STICKER_REQUEST_CODE);
return true;
} else if (item.getItemId() == R.id.main_remove_sticker) {
motionView.deletedSelectedEntity();
} else if (item.getItemId() == R.id.main_change_image) {
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(i, "Selecione uma imagem"), PICK_IMAGE);
} else if (item.getItemId() == R.id.main_save_croqui) {
takeScreenshot();
}
return super.onOptionsItemSelected(item);
}
public void takeScreenshot(){
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
String mPath = Environment.getExternalStorageDirectory().toString()+"/" + now + ".jpg";
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
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){
e.printStackTrace();
}
}
public void openScreenshot (File imageFile){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(imageFile);
intent.setDataAndType(uri, "image/");
startActivity(intent);
}
Any error appears in the log?
– Leticia Rosa
Nothing happens just like that...
– Guilherme do Valle
Log in the console the result of
getItemId()
, he’s showing up properly?– Costamilam
It is, but the method doesn’t seem to be called simply. I should put this. or something like that? I’m pretty beginner...
– Guilherme do Valle
Is getting into the
if
right? Log something in each if and see if everything is all right. If it is,takeScreenshot
is being called, Pack something in the console inside it?– Costamilam
That’s it, all ifs work, all other features are called. How do I test if the function is being called, simulating by smartphone?
– Guilherme do Valle
Whenever you have any problem the first thing to do is to put logs throughout the application, so you will know where your application is going, the values of the variables and where it stopped (last log). So you test whether a function or anything else is working as expected
– Costamilam
Like that? https://developer.android.com/studio/debug/? hl=en
– Guilherme do Valle
Exactly that "Record log messages in your code"
– Costamilam