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

时间:2019-06-10 19:48:50

标签: java android image android-intent

我有一个制作的GIF Maker应用,现在我想直接从该应用共享该gif到其他应用(GMAIL,WHATSAPP ETC)

如何从应用发送已创建的GIF

我用来在Gallery / Phone中保存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并共享给其他应用

有什么我可以用来解决此错误的方法吗?

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

在提供的代码段中使用变量filePath创建文件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!"));
}