我有一个简单的浮动按钮,可以打开相机,然后在主活动中显示图像。
我想在用户检索图像结果后立即删除该浮动按钮,因此应该在onActivityResult之后scannedImageView.setImageBitmap(bitmap)
我尝试使用,btnFloat.setVisibility(View.GONE);但是由于它是一个浮动按钮,所以我不知道如何在我的方法上调用它。
主要活动
ImageView scannedImageView;
public void init() {
FloatingActionButton fab = findViewById(R.id.fab_capture);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK && data != null) {
if (requestCode == OPEN_THING) {
Uri uri = data.getExtras().getParcelable(ScanConstants.SCANNED_RESULT);
Bitmap bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
getContentResolver().delete(uri, null, null);
scannedImageView.setImageBitmap(bitmap);
//btnFloat.setVisibility(View.GONE);
FileOutputStream outputStream = null;
File sdCard = new File(getExternalFilesDir(null).getAbsolutePath());
File directory = new File (sdCard.getAbsolutePath() +"/Scan Documents");
directory.mkdir();
String filename = String.format("d.jpg", System.currentTimeMillis());
File outFile = new File(directory, filename);
Toast.makeText(this, "Image Saved Successfully", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(outFile));
sendBroadcast(intent);
try{
outputStream = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
}catch (FileNotFoundException e)
{
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
将FloatingActionButton设为全局成员变量,然后可以在onActivityResult中访问它
public class MyActivity extends Activity {
...
FloatingActionButton fab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fab = findViewById(R.id.fab_capture);
}
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
...
fab.hide();
...
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。