Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

java - android how to share PDF,XIS,DOC,PNG from url

Here is my code

Glide.with(getContext())
                    .asBitmap().skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE)
                    .load(url)
                    .into(new SimpleTarget<Bitmap>(250, 250) {
                        @Override
                        public void onResourceReady(Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                            progressBar.setVisibility(View.GONE);
                            Intent intent = new Intent(Intent.ACTION_SEND);
                            intent.putExtra(Intent.EXTRA_TEXT, "Share");
                            String path = MediaStore.Images.Media.insertImage(getContext().getContentResolver(), resource, "", null);
                            Uri screenshotUri = Uri.parse(path);
                            intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
                            intent.setType("*/*");
                            startActivity(Intent.createChooser(intent, "Send to"));
                        }

                        @Override
                        public void onLoadFailed(Drawable errorDrawable) {
                            progressBar.setVisibility(View.GONE);
                            Toast.makeText(getContext(), "Something went wrong", Toast.LENGTH_SHORT).show();
                            super.onLoadFailed(errorDrawable);
                        }

                        @Override
                        public void onLoadStarted(Drawable placeholder) {
                            progressBar.setVisibility(View.VISIBLE);
                            Toast.makeText(getContext(), "Sharing", Toast.LENGTH_SHORT).show();
                            super.onLoadStarted(placeholder);
                        }
                    });

I am implementing share option in my android application using Url. The Url contains all types of files like PDF,PNG,DOC,DOCX,etc,...I tried to share PNG file among other apps and its working fine. The issue is I cannot able to share pdf and other format files except image.

Please help me to share all the MIME TYPE files

question from:https://stackoverflow.com/questions/65640861/android-how-to-share-pdf-xis-doc-png-from-url

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

1 Answer

0 votes
by (71.8m points)

May this example help you, I used this code for Sending PDF files, you can send mutiple by using array and Intent.ACTION_SEND_MULTIPLE:

public void shareFile(File file) {
        Uri uri = FileProvider.getUriForFile(mContext, com.example.app, file);
        shareFile(uri);
    }

Call shareFile function and pass URI

private void shareFile(Uri uri) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_TEXT, mContext.getString(R.string.file_with_message));
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setType("application/pdf");
        mContext.startActivity(Intent.createChooser(intent,"Select app to send file");
    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...