开发者社区> 问答> 正文

如何使浮动按钮消失在android中?

我有一个简单的浮动按钮,可以打开相机,然后在主活动中显示图像。

我想在用户检索图像结果后立即删除该浮动按钮,因此应该在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();
             }
         }
     }
 }

展开
收起
Puppet 2020-01-15 10:41:14 497 0
1 条回答
写回答
取消 提交回答
  • 将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();
       ...
     }
    }
    
    2020-01-15 10:41:40
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
手机卫士性能优化方案-Android篇 立即下载
58同城Android客户端Walle框架演进与实践之路 立即下载
Android内存泄漏自动化链路分析组件Probe 立即下载