如何使静态图像不可共享 android
How to make a static image not sharable android
当我在应用程序启动时单击它时,我有一个共享按钮方法可以工作它会要求共享我 activity_main
上显示的第一张图片,但我不想共享该图片。
当用户用相机拍照时,它会覆盖第一张图片,然后我才想分享那张照片。
也就是说我需要某种条件。我错过了什么?
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" />
MainActivity
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);
}
1) 删除此行 app:srcCompat="@drawable/ic_camera_alt_black_24dp"
或者不要从您的图像视图中绘制,最好的方法是使用您拍摄的图像创建新字段
2) 开始意图 finally {}
当我在应用程序启动时单击它时,我有一个共享按钮方法可以工作它会要求共享我 activity_main
上显示的第一张图片,但我不想共享该图片。
当用户用相机拍照时,它会覆盖第一张图片,然后我才想分享那张照片。
也就是说我需要某种条件。我错过了什么?
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" />
MainActivity
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);
}
1) 删除此行 app:srcCompat="@drawable/ic_camera_alt_black_24dp"
或者不要从您的图像视图中绘制,最好的方法是使用您拍摄的图像创建新字段
2) 开始意图 finally {}