You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android实现仅分享相机拍摄图片:排除初始静态图的条件设置问题

Fix: Share Button Only Works After Capturing a Photo

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:

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.

  1. Add the flag to your MainActivity:
private boolean isPhotoCaptured = false;
private ImageView scannedImageView;
  1. Initialize your ImageView in onCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    scannedImageView = findViewById(R.id.scanned_image);
}
  1. When the user captures a photo (in your camera result handling code, like onActivityResult or your camera callback), set the flag to true:
// Example: After setting the captured bitmap to scannedImageView
scannedImageView.setImageBitmap(capturedBitmap);
isPhotoCaptured = true;
  1. 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.

  1. 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();
}
  1. 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 11:37:50