从imageview滑动共享加载的图像

时间:2018-02-28 06:07:42

标签: android imageview android-glide

我的imageview小部件上有一个加载的图像,它是从滑动库加载的。我想使用共享意图将该图像共享给其他应用程序。我已经尝试了各种可能性而没有任何成功请帮忙。

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"));
            }
        });

1 个答案:

答案 0 :(得分:1)

  

将活动上下文传递给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);
        }