使用共享 Intent 在 android 中共享图像时遇到问题?

问题描述

我有图像厨房应用程序,因为我可以从服务器加载所有图像,没有任何问题。当我选择其中一个图库时,它将在全屏活动中显示图像。我希望能够全屏共享图像。我使用了以下代码,它适用于 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);

}

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)