【问题标题】:How to make Intent run after the above code execution id completed上述代码执行id完成后如何让Intent运行
【发布时间】:2017-06-23 16:29:13
【问题描述】:

我正在尝试制作一个应用程序来在 android studio 中剪切视频,然后将其分享给某个应用程序。但是共享似乎在切割过程完成之前就已经发生了

我的代码:

vidUris.add(Uri.fromFile(new File(dest.getAbsolutePath())));
String[] complexCommand = {"-i", yourRealPath, "-ss", "" + startMs, "-t", ""+leng , dest.getAbsolutePath()};
execFFmpegBinary(complexCommand);

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, vidUris);
shareIntent.setType("video/*");
startActivity(shareIntent);

【问题讨论】:

    标签: android android-sharing


    【解决方案1】:

    请检查 execFFmpegBinary 是否为异步方法。

    【讨论】:

      【解决方案2】:

      所以你需要一个回调函数,一旦切割完成就会被调用。这样您就可以启动共享意图。

      要实现这种行为,您可以考虑使用这样的界面。

      public interface CuttingCompleted {
          void onCuttingCompleted(String[] vidUris); 
      }
      

      现在使用AsyncTask 在后台线程中进行切割,当它完成时将结果传递给回调函数以进一步执行您的代码流。

      public class CuttingVideoAsyncTask extends AsyncTask<Void, Void, String[]> {
      
          private final Context mContext;
          public CuttingCompleted mCuttingCompleted;
      
          CuttingVideoAsyncTask(Context context, CuttingCompleted listener) {
              // Pass extra parameters as you need for cutting the video
              this.mContext = context;
              this.mCuttingCompleted = listener;
          }
      
          @Override
          protected String[] doInBackground(Void... params) {
              // This is just an example showing here to run the process of cutting. 
              String[] complexCommand = {"-i", yourRealPath, "-ss", "" + startMs, "-t", ""+leng , dest.getAbsolutePath()};
              execFFmpegBinary(complexCommand);
              return complexCommand;
          }
      
          @Override
          protected void onPostExecute(String[] vidUris) {
              // Pass the result to the calling Activity
              mCuttingCompleted.onCuttingCompleted(vidUris);
          }
      
          @Override
          protected void onCancelled() {
              mCuttingCompleted.onCuttingCompleted(null);
          }
      }
      

      现在,您需要从您的Activity 实现接口,以便在切割过程完全完成时开始您的共享意图。

      public class YourActivity extends Activity implements CuttingCompleted {
      
          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              // ... Other code
      
              new CuttingVideoAsyncTask(this, this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
          }
      
          @Override
          public void onCuttingCompleted(String[] vidUris) {
              Intent shareIntent = new Intent();
              shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);
              shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, vidUris);
              shareIntent.setType("video/*");
              startActivity(shareIntent);
          }
      }
      

      【讨论】:

      • 尝试使用 AsyncTask 但调用切割后活动立即关闭。给 D/AndroidRuntime: 关闭 VM I/Process: 发送信号。 PID:2346 SIG:9
      猜你喜欢
      • 2011-04-10
      • 1970-01-01
      • 1970-01-01
      • 2018-11-27
      • 2019-11-20
      • 1970-01-01
      • 2013-08-21
      • 2022-01-10
      • 2012-02-08
      相关资源
      最近更新 更多