Android实现仅分享相机拍摄图片:排除初始静态图的条件设置问题
Hey there! The issue here is that your current check if(myDrawable == null) never triggers—because your ImageView starts with a default drawable (ic_camera_alt_black_24dp) set via app:srcCompat. So even before the user takes a photo, scannedImageView.getDrawable() will always return a non-null value, which is why it’s sharing the default icon right away.
Here are two straightforward fixes to make sure sharing only works after the user captures a photo:
Option 1: Use a Boolean Flag (Recommended)
Add a simple boolean variable to track whether a photo has been captured. Update it when the user successfully takes and sets a photo to the ImageView.
- Add the flag to your
MainActivity:
private boolean isPhotoCaptured = false; private ImageView scannedImageView;
- Initialize your
ImageViewinonCreate:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); scannedImageView = findViewById(R.id.scanned_image); }
- When the user captures a photo (in your camera result handling code, like
onActivityResultor your camera callback), set the flag totrue:
// Example: After setting the captured bitmap to scannedImageView scannedImageView.setImageBitmap(capturedBitmap); isPhotoCaptured = true;
- Update your share method’s condition to check this flag:
public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_share) { if(!isPhotoCaptured) { Toast.makeText(MainActivity.this, "Please capture a photo first!", Toast.LENGTH_SHORT).show(); } else { // Keep your existing share logic here Drawable myDrawable = scannedImageView.getDrawable(); Bitmap bitmap = ((BitmapDrawable) myDrawable).getBitmap(); try { Uri file; File file2 = new File(getExternalCacheDir(), "myImage.png"); FileOutputStream fOut = new FileOutputStream(file2); bitmap.compress(Bitmap.CompressFormat.PNG, 80, fOut); fOut.flush(); fOut.close(); file2.setReadable(true, false); Intent intentx = new Intent(Intent.ACTION_SEND); intentx.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if (Build.VERSION.SDK_INT < 21) { file = Uri.fromFile(file2); } else { String authority = getPackageName() + ".provider"; file = FileProvider.getUriForFile(this, authority, file2); } intentx.putExtra(Intent.EXTRA_STREAM, file); intentx.setType("image/png"); Intent intx = Intent.createChooser(intentx, "Share Image"); for (ResolveInfo resolveInfo : getPackageManager().queryIntentActivities(intx, PackageManager.GET_META_DATA)) { grantUriPermission(resolveInfo.activityInfo.packageName, file, Intent.FLAG_GRANT_READ_URI_PERMISSION); } startActivity(intx); } catch (FileNotFoundException e) { e.printStackTrace(); Toast.makeText(MainActivity.this, "File not found", Toast.LENGTH_SHORT).show(); } catch (Exception e2) { e2.printStackTrace(); } } } return super.onOptionsItemSelected(item); }
Option 2: Compare Drawables to the Default
If you prefer not to use a flag, you can store a reference to the default drawable and check if the current drawable matches it.
- Store the default drawable in
onCreate:
private Drawable defaultDrawable; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); scannedImageView = findViewById(R.id.scanned_image); defaultDrawable = scannedImageView.getDrawable(); }
- Update the share condition to check if the current drawable is the default one:
public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == R.id.action_share) { Drawable myDrawable = scannedImageView.getDrawable(); if(myDrawable != null && myDrawable.equals(defaultDrawable)) { Toast.makeText(MainActivity.this, "Please capture a photo first!", Toast.LENGTH_SHORT).show(); } else { // Keep your existing share logic here } } return super.onOptionsItemSelected(item); }
Either approach will prevent sharing the default icon and only allow sharing after the user has captured a photo.
内容的提问来源于stack exchange,提问作者Ishak Benaissa

