Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

java - Having problem Sharing an image using sharing Intent to share images in android?

I have image galley app in that I have all images load from server without any problem. When I select one of the gallery it will show the image in a fullscreen activity. I want to be able to share the image in fullscreen. I have used the below code and it works on Facebook, Gmail ..etc but when I try to share on WhatsApp the emulator crashes. I also get 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);

}

}

question from:https://stackoverflow.com/questions/66054097/having-problem-sharing-an-image-using-sharing-intent-to-share-images-in-android

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...