繁体   English   中英

如何在Android Studio中通过Intent分享GIF

[英]How to Share GIF via Intent In Android Studio

我有一个GIF制作应用程序,现在我想直接从应用程序分享到其他应用程序(GMAIL,WHATSAPP等)

如何从应用程序发送创建的GIF

代码我用于在图库/手机中保存GIF

private void saveGif() {
    new AsyncTask<Void, Integer, Void>() {
        ProgressDialog progressDialog;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressDialog = new ProgressDialog(GifMakerActivity.this);
            progressDialog.setMax(100);
            progressDialog.setMessage("Please wait..");
            progressDialog.setCancelable(false);
            progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            progressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... voids) {
            File filePath;
            if (TextUtils.isEmpty(gifPath)) {
                int random = (int) (Math.random() * 9000) + 1000;
                File gifMaker = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Gif Maker");
                if (!gifMaker.mkdir()) {
                    Log.e("GifMakerActivity: ", "Directory doesn't exist");
                }
                filePath = new File(gifMaker, "GifMaker_" + random + ".gif");



            } else {
                filePath = new File(gifPath);

            }

            try {
                int size = bitmaps.size();
                int w = bitmaps.get(0).getWidth();
                int h = bitmaps.get(0).getHeight();
                GifEncoder gifEncoder = new GifEncoder();
                gifEncoder.init(w, h, filePath.getAbsolutePath(), GifEncoder.EncodingType.ENCODING_TYPE_FAST);
                for (Bitmap bitmap : bitmaps) {
                    gifEncoder.encodeFrame(Bitmap.createScaledBitmap(bitmap, w, h, false), duration);
                    publishProgress(100/size);
                }
                gifEncoder.close();
            } catch (FileNotFoundException e) {}

            if (progressDialog.getProgress() <= progressDialog.getMax()) {
                publishProgress(progressDialog.getMax() - progressDialog.getProgress());
            }

            /*ByteArrayOutputStream bos = new ByteArrayOutputStream();
            AnimatedGifEncoder encoder = new AnimatedGifEncoder();
            encoder.start(bos);
            Log.d("duration: ", duration + "");
            encoder.setDelay(duration);
            encoder.setRepeat(0);

            for (Bitmap bitmap : bitmaps) {
                encoder.addFrame(bitmap);
            }

            encoder.finish();

            FileOutputStream outputStream;
            try {
                outputStream = new FileOutputStream(gifMaker.getAbsolutePath());
                outputStream.write(bos.toByteArray());
                outputStream.flush();
                outputStream.close();
                bos.flush();
                bos.close();
            } catch (FileNotFoundException e) {
            } catch (IOException e) {
            }*/

            /*AnimatedGifWriter writer = new AnimatedGifWriter(true);
            try {
                OutputStream os = new FileOutputStream(gifMaker);
                writer.prepareForWrite(os, -1, -1);
                for (Bitmap bitmap : bitmaps) {
                    writer.writeFrame(os, bitmap);
                }
                writer.finishWrite(os);
            } catch (Exception e) {

            }*/

            MediaScannerConnection.scanFile(GifMakerActivity.this,
                    new String[] { filePath.getAbsolutePath() }, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                        public void onScanCompleted(String path, Uri uri) {
                            //Log.i("ExternalStorage", "Scanned " + path + ":");
                            //Log.i("ExternalStorage", "-> uri=" + uri);
                        }
                    });

            return null;
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            progressDialog.incrementProgressBy(values[0]);
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            if (progressDialog.getProgress() == progressDialog.getMax()) {
                progressDialog.dismiss();

                sendcomletionnotification();
            }
        }
    }.execute();
}

如何共享用户创建并与其他应用共享的GIF

有什么方法可以用来解决这个错误。

那么,您可以尝试搜索应用程序的文件并使用intent方法与其他应用程序共享尝试在您的方法中发送文件:

Intent i = new Intent();
i.setAction(Intent.ACTION_SEND);
i.setType("img/*");
i.setPackage("com.whatsapp");
startActivity(i);

使用提供的代码段中的变量filePath创建File Uri以保存数据流,然后使用ACTION_SEND提供此图像数据。 只需调用此方法:

private void shareImage() {
    Intent share = new Intent(Intent.ACTION_SEND);

   // For sharing png image only:
   // setType("image/png"); 
   // For sharing jpeg image only: 
   // setType("image/jpeg");

    share.setType("image/*");

    Uri uri = Uri.fromFile(filePath);
    share.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity( Intent.createChooser(share, "Share this image to your friends!") );
   }

要将某些引用或其他内容作为文本共享,请使用以下方法:

private void shareTextUrl() {
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("text/plain");
    share.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
    share.putExtra(Intent.EXTRA_SUBJECT, "Avoiding complexity reduces bugs");
    share.putExtra(Intent.EXTRA_TEXT, "Quote by Linus Torvalds");
    startActivity(Intent.createChooser(share, "Share this quote!"));
}

暂无
暂无

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

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