首页 > 解决方案 > 如何使静态图像不可共享android

问题描述

当我在应用程序启动时单击它时,我有一个共享按钮方法工作它会要求共享我显示的第一张图像,activity_main但我不想共享该图像。

当用户用相机拍照时,它会覆盖第一张图像,然后我才想分享那张照片。

换句话说,我需要某种条件。我错过了什么?

活动主

 <ImageView
        android:id="@+id/scanned_image"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:scaleType="center"
        app:srcCompat="@drawable/ic_camera_alt_black_24dp" />

主要活动

 public boolean onOptionsItemSelected(MenuItem item) {
     int id = item.getItemId();
     if (id == R.id.action_share) {
Drawable myDrawable = scannedImageView.getDrawable();
         if(myDrawable == null) {
             Toast.makeText(MainActivity.this, "Load Image First...", Toast.LENGTH_SHORT).show();

         } else {
             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("android.intent.action.SEND");
                 intentx.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                 if (Build.VERSION.SDK_INT < 21) {
                     file = Uri.fromFile(file2);
                 } else {
                     StringBuilder sb = new StringBuilder();
                     sb.append(getPackageName());
                     sb.append(".provider");
                     file = FileProvider.getUriForFile(this, sb.toString(), file2);
                 }
                 Intent intx = Intent.createChooser(intentx, "Share Image");
                 for (ResolveInfo resolveInfo : getPackageManager().queryIntentActivities(intx, PackageManager.GET_META_DATA)) {
                     grantUriPermission(resolveInfo.activityInfo.packageName, file, FLAG_GRANT_READ_URI_PERMISSION);
                 }
                 intentx.putExtra("android.intent.extra.STREAM", file);
                 intentx.setType("image/png");
                 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);
}

标签: javaandroidimageconditional-statementsshare

解决方案


1)删除这条线app:srcCompat="@drawable/ic_camera_alt_black_24dp"或不要从您的图像视图中绘制,最好的方法是使用您拍摄的图像创建新字段 2)开始意图finally {}


推荐阅读