How to Find Video File in Another Module - Android Studio - android

My project includes two modules, one main module with all the XML, Kotlin files, and images, and another with all the video files. I had to do this because my app has like 20 videos and it exceeds that 150 MB cap that is put on the app bundles. Anyway, I have am trying to play a video through a video view, and it worked before by just finding the filepath. Now, I'm not sure how to access the video in another module, and when I try to get the filepath it can't find it. I've already set up the dependencies, so I don't know what the issue is.
Here is an example of what I had before I moved the videos into the new module, and this worked:
val intent = Intent(requireActivity(), WatchActivity::class.java)
intent.putExtra("filePath", "android.resource://${requireActivity().packageName}/${R.raw.addeventvideo}")
requireActivity().startActivity(intent)
If you're wondering, that code is inside of an onClickListener for a button so that it opens a new activity with a videoview that plays the video with the filepath I pass into the intent.
Thanks for any help with this.

Related

Open an Image in Internal Storage Using Content Provider

Let's say I package an image with my app and I want to open it with the default image viewer/whatever image viewer the user has chosen to be the default. How would I do that?
There's already this post: Open an image using URI in Android's default gallery image viewer but many of the answers are obsolete because due to the introduction of android N, a content provider must be used.
The only answer I can find is this one:
File file = ...;
final Intent intent = new Intent(Intent.ACTION_VIEW)//
.setDataAndType(VERSION.SDK_INT >= VERSION_CODES.N ?
android.support.v4.content.FileProvider.getUriForFile(this,getPackageName() + ".provider", file) : Uri.fromFile(file),
"image/*").addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
but, according to the author of this solution, this code only works for images stored externally, not ones that may be packaged with my app.
You won't be able to open an image packaged with the app (in drawable or whatever resources) through an external application. You should first copy it into (at least) an internal file storage. After that you can implement a FileProvider to provide access to this file.
Let me know if you need more details on this. Hope it helped.

Can't play video through intent

My app has a photo gallery that displays a constant list of images. I need to add one video to that list (just one, provided by me). This video is inside an expansion file. I would like to let the user decide the video player he wants to use to play the video. So I went for the intent approach:
public void playVideo(View view){
Intent videoint = new Intent(Intent.ACTION_VIEW);
Uri uri = CustomAPEZProvider.buildUri("test.3gp");
Log.d("TEST", uri.toString());
videoint.setDataAndType(uri, "video/*");
startActivity(videoint);
}
My CustomAPEZProvider is the following:
public class CustomAPEZProvider extends APEZProvider {
private static final String AUTHORITY = "com.myapp.package.provider";
#Override
public String getAuthority() {
return AUTHORITY;
}
public static Uri buildUri(String path) {
StringBuilder contentPath = new StringBuilder("content://");
contentPath.append(AUTHORITY);
contentPath.append(File.separator);
contentPath.append(path);
return Uri.parse(contentPath.toString());
}
}
Also, I added this to my manifest:
<provider android:name="com.myapp.package.CustomAPEZProvider"
android:authorities="com.myapp.package.provider" >
android:exported="true"
android:multiprocess="true">
<meta-data
android:name="mainVersion"
android:value="4"/>
</provider>
The provider has this meta-data because the expansion file version differs from the apps version code.
I understand that the file is being found, but the video players are not able to play it. They are launching the can't play this video window (and no errors). I tested it on many devices and with different kinds of videos. The 3gp video I'm using to test can be played just fine from the phone's native gallery.
Line 3 on the playVideo method is printing this
content://com.myapp.package.provider/test.3gp
This is correct, right?
The expansion file has no folders, files are just thrown at root.
Also, I actually need to play this test.3gp video from the patch expansion file. Will there be any difference in that case? I'm eliminating this obstacle for now. I know I should add it to the provider's meta-data.
Some extra information: the expansion file has several audio files that I'm being able to play using a MediaPlayer without any issues. Of course, this is different because in that case I'm doing it by getting an AssetFileDescriptor to the file inside the obb expansion file, whereas with the video I need an Uri, which changes everything.
I read lots of questions with similar problems, but they were not helpful. Does anyone had the same problem?
Workarounds are also welcome. For example, I could accept to use a VideoView if needed.
UPDATE
I've just realised that the video player is not working, even if the file is a resource (inside drawable, raw, or whatever). I did manage to play the video with the code below:
public void playVideo(View view){
Uri uri = CustomAPEZProvider.buildUri("test.3gp");
getWindow().setFormat(PixelFormat.TRANSLUCENT);
VideoView videoHolder = new VideoView(this);
videoHolder.setMediaController(new MediaController(this));
videoHolder.setVideoURI(uri);
setContentView(videoHolder);
videoHolder.start();
}
But this is not exactly what I want, I'd really like to allow the user to choose the video player of his preference. Mainly because I want to free myself from the responsibility to code a nice-to-look-at video player.
I don't think the problem lies anywhere in your code. Or, if it does, it's not your biggest problem.
I think your biggest problem is using a 3GP file. That format is not supported by all devices. You're better off with an MP4. And even then, make sure that it's encoded with a CODEC that all Android devices understand.
I had the same problem (video played without problem with native gallery, but not through intent). How the problem is solved is to add the following line to manifest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
and give the permission to the installed app.

How to get actual path to video from onActivityResult

I have a video that I save to .../Movies/MyApp/abcde.mp4. So I know where it is. When I load it through my app using an implicit intent to ACTION_GET_CONTENT, the path is returned as content:/media/external/video/media/82 when I do
data.getData().toString()
The problem with that path is that it works when I try to access it with MediaRecorder as
mVideoView.setVideoURI(Uri.parse(mVideoStringPath))
However if I try to convert it to a path in another thread (for a job queue), the file is not found
new File(mVideoStringPath)
when I use the technique (copy and paste) described at How to get file path in onActivityResult in Android 4.4, still get the error
java.lang.RuntimeException: Invalid image file
Also per my logging, the new technique shows the path to the video as
video path: /storage/emulated/0/Movies/MyApp/abc de.mp4
notice the space in abc de.mp4. that indeed is the name of the file. And the phone's camera app has no trouble playing
However if I try to convert it to a path in another thread (for a job queue), the file is not found
That is because it is not a path to a file. It is a Uri, which is an opaque handle to some data.
How to get actual path to video from onActivityResult
You don't. You use the Uri. There is no requirement that the Uri point to a file. There is no requirement that the Uri, if it happens to represent a file, represent one that you have direct filesystem access to.
you need to escape the space the the file path in order to construct a File object from it.
filepath.replace(" ", "\\ ");

Air for android ignores external swf files

I am working on some app that loads external swfs from the application directory
the problem is that some swf files get loaded correctly and others give ioerror url not found
i put the paths in an array and use a loader to load the path
var arr:Array = ["Games/1.swf", "Games/2.swf"];
var loader:Loader = new Loader();
loader.load(new URLRequest(arr[0]));
this is just example and it is the same in loading all the files but does not work on all files.
what would be the problem?
Firstly, whenever you load something, listen for the appropriate error events so you know what's going on:
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
When using mobile, you should use the app:// shorthand as a reference to the application directory (which I assume is where your Game folder is. Relative paths do not usually work.
So it should look like this:
var arr:Array = ["app://Games/1.swf", "app://Games/2.swf"];
For more information/options, you can look at my answer to this question

Android Video view: Unable to play video files with '%' character in filename

I am trying to play a file example name : 'sample%20video.mp4' inside a 'VideoView'.
The file does'nt play showing an error :
Can't play video
There is no problem with the video file, as it works fine when removing the '%' from the file name.
Note: When launching the video('sample%20video.mp4') from the file location using apps like 'Photos' , 'Video player' plays fine without any issue.
Anybody knows the reason for this kind of behavior??
Are you passing the filename to Uri.parse() without first calling Uri.encode()? If so then that could be the reason for the problem, you need to encode it first to handle any special characters:
VideoView videoView = findViewById(R.id.videoView);
videoView.setVideoURI(Uri.parse(Uri.encode(videofilepath)));

Categories

Resources