简体   繁体   English

通过imageview滑动共享已加载的图像

[英]Share loaded image with glide from imageview

I have a loaded image on my imageview widget which was loaded from glide library. 我的imageview小部件上有一个已加载的图像,该图像是从glide库加载的。 I want to use a share intent to share that image to other applications. 我想使用共享意图将该图像共享给其他应用程序。 I have tried various possibilities without any success. 我尝试了各种可能性,但都没有成功。 Please help. 请帮忙。

public class BookstorePreviewActivity extends AppCompatActivity {
    ImageView imageView;
    LinearLayout mShare;
    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_bookstore_preview);
        imageView  = findViewById(R.id.image_preview_books);
        mShare= findViewById(R.id.download_books);
        mShare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
// fetching image view item from cache
                imageView.setDrawingCacheEnabled(true);
                Bitmap bitmap = imageView.getDrawingCache();
                File root = Environment.getExternalStorageDirectory();
                File cachePath = new File(root.getAbsolutePath());
                try {
                    cachePath.createNewFile();
                    FileOutputStream outputStream = new FileOutputStream(cachePath);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
// sharing image to other applications (image not found)
                Intent share = new Intent(Intent.ACTION_SEND);
                share.setType("image/*");
                share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(cachePath));
                startActivity(Intent.createChooser(share, "Share via"));
            }
        });

Just Pass activity context to the takeScreenShot activity it will work!!! 只要将活动上下文传递给takeScreenShot活动,它将起作用!!!

public static Bitmap takeScreenShot(Activity activity) {
            View view = activity.getWindow().getDecorView();
            view.setDrawingCacheEnabled(true);
            view.buildDrawingCache();
            Bitmap b1 = view.getDrawingCache();
            Rect frame = new Rect();
            activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
            int statusBarHeight = frame.top;

            //Find the screen dimensions to create bitmap in the same size.
            DisplayMetrics dm = activity.getResources().getDisplayMetrics();
            int width = dm.widthPixels;
            int height = dm.heightPixels;


            Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);
            view.destroyDrawingCache();
            takeScreen(b,activity);
            return b;
        }

        public static void takeScreen(Bitmap bitmap,Activity a) {
            //Bitmap bitmap = ImageUtils.loadBitmapFromView(this, view); //get Bitmap from the view
            String mPath = Environment.getExternalStorageDirectory() + File.separator + "tarunkonda" + System.currentTimeMillis() + ".jpeg";
            File imageFile = new File(mPath);

            try {
                OutputStream fout = new FileOutputStream(imageFile);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
                fout.flush();
                fout.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            openScreenshot(imageFile,a);


        }
        private static void openScreenshot(File imageFile,Activity activity) {
            Intent intent = new Intent(activity,ImageDrawActivity.class);
            intent.putExtra(ScreenShotActivity.PATH_INTENT_KEY,imageFile.getAbsolutePath());
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            activity.startActivity(intent);
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM