我有一个简单的浮动按钮,可以打开照相机,然后在主要活动中显示图像。
我想在用户检索图像结果后立即删除该浮动按钮,因此应该在onActivityResult
之后为scannedImageView.setImageBitmap(bitmap)
我尝试使用btnFloat.setVisibility(View.GONE);
,但由于它是一个浮动按钮,所以我不知道如何在我的方法上调用它。
MainActivity
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();
...
}
}