1

我有图像厨房应用程序,因为我可以从服务器加载所有图像,没有任何问题。当我选择其中一个画廊时,它将在全屏活动中显示图像。我希望能够全屏共享图像。我使用了下面的代码,它可以在 Facebook、Gmail 等上运行,但是当我尝试在 WhatsApp 上分享时,模拟器崩溃了。我也得到 SecurityException: Permission Denial: reading androidx.core.content.FileProvider uri content

private static final String TAG = "FullScreen";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_full_screen);

    StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
    StrictMode.setVmPolicy(builder.build());

   // String imageurl = getIntent().getStringExtra("image_url");
    //Log.e ("Image Url :", ""+imageurl);
    img = (ImageView) findViewById(R.id.image_view);

    getIncomingIntent();

    button = (Button) findViewById(R.id.shareme);
    button.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            Drawable drawable=img.getDrawable();
            Bitmap bitmap=((BitmapDrawable)drawable).getBitmap();


            try {
                File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + ".png");
                Log.e ("Local Image:", ""+file);
                FileOutputStream fOut = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
                fOut.flush();
                fOut.close();
               // file.setReadable(true, false);
                final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
                Uri photoURI = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID +".provider", file);
                intent.putExtra(Intent.EXTRA_STREAM, photoURI);
                intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION );
                intent.setType("image/*");
                startActivity(Intent.createChooser(intent, "Share image via"));
            } catch (Exception e) {
                e.printStackTrace();
            }


        }



    });
}

private void getIncomingIntent(){
    if(getIntent().hasExtra("image_url")){
        String imageurl = getIntent().getStringExtra("image_url");
        setImage(imageurl);
       // Log.e ("Image Url from server:", ""+imageurl);
    }
}

private void setImage(String imageUrl){
    ImageView image = findViewById(R.id.image_view);
    Glide.with(this)
            .asBitmap()
            .load(imageUrl)
            .into(img);

}

}

4

0 回答 0