将视频转换为 GIF
Posted
技术标签:
【中文标题】将视频转换为 GIF【英文标题】:Converting a video to a GIF 【发布时间】:2020-03-17 13:00:15 【问题描述】:我正在尝试让我的应用程序接受来自手机库的视频以上传并转换为 GIF 格式。我的代码虽然给出了这个构建错误:-
error: <anonymous com.example.bim.Video2gif$2> is not abstract and does not override abstract method onReschedule(String,ErrorInfo) in UploadCallback
还有关于我的 onActivityResult 方法的警告:-
Overriding method should call super.onActivityResult
代码如下:-
public class Video2gif extends AppCompatActivity
private Button uploadBtn;
private ProgressBar progressBar;
private int SELECT_VIDEO = 2;
private ImageView img1;
private DownloadManager downloadManager;
private Button download_btn;
private String gifUrl;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video2gif);
MediaManager.init(this);
progressBar = findViewById(R.id.progress_bar);
MediaManager.init(this);
img1 = findViewById(R.id.img1);
uploadBtn = findViewById(R.id.uploadBtn);
uploadBtn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
pickVideoFromGallery();
private void pickVideoFromGallery()
Intent GalleryIntent = new Intent();
GalleryIntent.setType("video/*");
GalleryIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(GalleryIntent,
"select video"), SELECT_VIDEO);
);
@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data)
if (requestCode == SELECT_VIDEO && resultCode == RESULT_OK)
Uri selectedVideo = data.getData();
MediaManager.get()
.upload(selectedVideo)
.unsigned("myid")
.option("resource_type", "video")
.callback(new UploadCallback()
@Override
public void onStart(String requestId)
progressBar.setVisibility(View.VISIBLE);
Toast.makeText(Video2gif.this,
"Upload Started...", Toast.LENGTH_SHORT).show();
public void onProgress()
public void onSuccess(String requestId, Map resultData)
Toast.makeText(Video2gif.this, "Uploaded Succesfully",
Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
uploadBtn.setVisibility(View.INVISIBLE);
String publicId = resultData.get("public_id").toString();
gifUrl = MediaManager.get().url().resourceType("video")
.transformation(new Transformation().videoSampling("25")
.delay("200").height(200).effect("loop:10").crop("scale"))
.resourceType("video").generate(publicId+".gif");
Glide.with(getApplicationContext()).asGif().load(gifUrl).into(img1);
download_btn.setVisibility(View.VISIBLE);
public void onError(String requestId, ErrorInfo error)
Toast.makeText(Video2gif.this,
"Upload Error", Toast.LENGTH_SHORT).show();
Log.v("ERROR!!", error.getDescription());
);
我还使用 Cloudinary 来帮助将视频处理为 GIF。任何帮助将不胜感激,非常感谢!
【问题讨论】:
【参考方案1】:当您在 android 中使用接口时,您需要在您的活动/片段中包含(覆盖)它们包含的回调方法。此外,覆盖某些系统方法需要调用它们的 super,这意味着许多活动在相互继承时可能正在侦听相同的回调。通过在这些回调中添加 super ,您可以让结果通过所有回调。因此,对于OnActivityResult
,只需在您的方法中添加以下行:
super.onActivityResult(requestCode, resultCode, data);
对于onReschedule
,您可以让 Android Studio 为您生成它。只需转到 Code->Generate-Override Methods 并选择 onReschedule
【讨论】:
您的答案很准确,解决了我的两个问题,我也使用这种方式为 onProgress 生成了覆盖方法,谢谢!!!以上是关于将视频转换为 GIF的主要内容,如果未能解决你的问题,请参考以下文章