So I hope it's not a repeated question but, from the following code
File f = new File(Environment.getExternalStorageDirectory(), TRYVID);
Uri uri = Uri.fromFile(f);
mc = new MediaController(this);
mp.setMediaController(mc);
mp.setVideoPath("/sdcard/try2.mp4");
this is part of a function that's called when a button is pressed, what i'm hoping to achieve is that when the user presses a key, the video plays but i've learned that the videoview does not play anything from the raw folder so i copied the video into the sdcard, but then after i press on the button on the emulator, it just crashes says it has to be close unexpectedly. I tried both the .setVideoPath as well as the .setUri but both does not work hmm anyone can point to my problem here?
Ok so first off you need to make sure that you use the .setAnchorView(View v) on your mediaController or else it wont correctly control the videoView. Also your missing your .start() to actually start the video. Having recently done something similar with streaming from an rstp video file i can tell you there there is a chance its not working because your running it on an emulator, the video playback on AVD's often doesn't work. Try running it on a physical device if you have access to one, also read the logcat to get a better idea of where the errors are happening.
I hope this helps.
For playing video files an from SD card you can try this:
String filepath = Environment.getExternalStorageDirectory()+"/a.mp4";
VideoView vv = new VideoView(getApplicationContext());
setContentView(vv);
vv.setVideoPath(filepath);
vv.setMediaController(new MediaController(this));
vv.requestFocus();
vv.start();
Try this below code this wii surely solve your problem,
Make videoView,
VideoView videoView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
videoView = (VideoView)findViewById(R.id.VideoView);
videoView.setVideoPath("/sdcard/blonde_secretary.3gp");
videoView.start();
}
I wish it will help you.
I had the same question and found the solution. My code in the link works fine.
Check this question of mine
Related
I'm currently working on making an Android app, but have been having some trouble. I want to be able to push a button with the title of the document as it's TextView and then have that document open to be read. I've looked around for guides but everything I've found is either out of date or doesn't explain any of the code shown. Does anyone know how I can put such a thing in my app? At this point, I'm not even sure where to start with the process.
Note, I'm working in Java not Kotlin.
UPDATE: I was directed to a solution using intents. Now there seems to be an issue loading the file. My code is this:
public class atotf_pdf extends literature {
File file = new File("/storage/emulated/0/AToTF Preview.pdf");
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
Uri pdfURI = FileProvider.getUriForFile(atotf_pdf.this, "net.whispwriting.whispwriting.provider", file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(pdfURI, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
Is it something here that's causing the error?
It looks like the file is somehow not being stored in the filesystem when the app is installed.
Just like #ruben said, you could use a WebView for such a task: https://developer.android.com/reference/android/webkit/WebView. For more information about using WebView to display content, you can take a look at this post: How can I display a pdf document into a Webview?
Another potential solution could be using intents, which has also been discussed before: How to open a PDF via Intent from SD card
Hopefully this helps!
Actually, I'm Developing a Video player. then I have small Requirement for the video player i.e., whenever my video player application is launched when clicking on the fab button how to play the last played video? Please anyone can help me..
Hai Priyank Actually How to Add the Volume Increase and Decrease options using seekbar on Swipeup(); and swipe down on onToucheventListener. I have no idea to add the above functionality if possible please tell me the process or please provide the sample Priyank.
Store your last played video uri or file path some where in shared preference or in database.
and on click play that uri or file in your video view. like below example
Ask anything if you not getting my point.
Store last Video :
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref",0);
Editor editor = pref.edit();
editor.putString("LastVideo", "path or Uri"); // Store your last video uri or file path
editor.commit();
Get last video :
String path = pref.getString("LastVideo", null); // getting String
You will get you last uri or path into path variable.
I have a Android App I have developed, It has been created from a successful ios App that was released and works fine. On one device it works correctly, no errors and does not crash. However on another device (newer, newer version of Android, faster processor) it crashes. Here are the memory errors when it crashes.
The app is designed to take a picture and to then use that picture as the background for a canvas, then a screen shot is taken. I can go through this process once, but then if I repeat the process and take another picture, the application crashes on exit of the camera API for the second time. And displays these errors. The application is Developed in Eclipse using Phonegap and Jquery mobile. I am unsure which parts of my code I should post to help this problem, but please feel free to ask if you feel some may be relevant.
Any help is really appreciated.
Okay, if you review my post on why we run out of memory on PhoneGap Android apps:
http://simonmacdonald.blogspot.ca/2012/07/change-to-camera-code-in-phonegap-190.html
You may find some tips when you use the camera.getPicture() command. Other than that I'm currently working on fixing some of these Camera issues by bringing everything in house instead of firing a camera intent.
You can make such trick.... To set picture as background use...
public void setbg(){
String pathName = Environment.getExternalStorageState() + "scm_pic.jpg";
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeFile(pathName);
BitmapDrawable bd = new BitmapDrawable(res, bitmap);
View view = findViewById(R.id.container);
view.setBackgroundDrawable(bd);
}
And then when you don't need this picture or want to #Override onPause methods or to switch another activity use...
private void unbindDrawables(View view) {
if (view.getBackground() != null) {
view.getBackground().setCallback(null);
}
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
unbindDrawables(((ViewGroup) view).getChildAt(i));
}
((ViewGroup) view).removeAllViews();
}
}
To use it in #Override use for examole...
unbindDrawables(findViewById(R.id.container));
System.gc();
if you use camera means Destination type must in FILE_URL in android. because DATA_URI gives base64 string so it gives out of memory error sometime. First u get URL from camera then Convert base64 format using filesystem concepts...
This is my first question on StackOverflow, and my english is poor, so please :D. I do this just for fun. I want to change the icon per user, letting users change the icon of the app. I think if I want to do this, I must read the apk self, unzip it, change che drawable/icon.png and rebuild it, at last to sign.
I try some code, but failed. The import reason is that, package sun.securate.* is not in Android Java framework. I write some code to do this, but it failed-_-
Who can help me? I can share my already written code.
Help me,Please!
Well another work around will be to create shortcut icon for Home Screen which launches the same Activity as the launcher. With this your launcher icon in drawer will be the actual one but on the home screen you will have different icon. You can also explore the idea of creating shortcut for other apps if you know the package info.
Ref:
For package info here are the doc.
PackageManager pkgMgr = getPackageManager();
For creating short cut here is the snippet (this is just for illustration):
// Setting the intent class ActivityClassToLaunch you need to try PackageManager
Intent i = new Intent(activity, ActivityClass.class);
i.setAction(Intent.ACTION_MAIN);
i.addCategory(Intent.CATEGORY_LAUNCHER);
Intent result = new Intent();
result.putExtra(Intent.EXTRA_SHORTCUT_INTENT, i);
result.putExtra(Intent.EXTRA_SHORTCUT_NAME, activity.getString(R.string.app_name));
ShortcutIconResource iconResource = null;
iconResource = ShortcutIconResource.fromContext(activity, R.drawable.new_app_icon);
result.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
result.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
activity.sendBroadcast(result);
If you just want user to change the app icon, why dont you load the icon from the SD card. And the icon that you ship with your package will be there if no icon is specified by user.
Here is the snippet that can help you.
onCreate(...)
setContentView(...);
setFeatureDrawable(Window.FEATURE_LEFT_ICON,
<your_drawable>);
....
}
If this does not work, try calling setFeatureDrawable before setContentView
You cannot change the launcher icon of a signed-and-sealed APK, except through a software upgrade.
I have some confusion regarding where you put regular java code in an android application.
I'm using the Eclipse SDK and by default when you create an application it makes a .java file with an OnCreate() method. Is this where I would put my code, inside this method?
Right now in my layout I have an imageButton, once this button is clicked I want to open a new WebView page that gets it's HTML code from the index.html file found in the assets folder. This is what I have so far...
Button button = (Button)findViewById(R.id.imagebutton1);
if(button.isPressed())
{
WebView webview = new WebView(null);
setContentView(webview);
try {
InputStream fin = getAssets().open("index.html");
byte[] buffer = new byte[fin.available()];
fin.read(buffer);
fin.close();
webview.loadData(new String(buffer), "text/html", "UTF-8");
} catch (IOException e) {
e.printStackTrace();
}
}
I have this block of code at the end of the onCreate() method right under the line:
setContentView(R.layout.main);
However, once I run the program it crashes and tells me that it failed to start. I'm assuming that it has to do with the fact that the code is in the onCreate. I know that its not where I'm supposed to put it, but I can't think of anywhere else the code should go. Am I supposed to make a new .java file and have a main method there? I'm currently taking classes for C++ and C# so this android thing is still new to me.
Have you done the tutorials? If not I would start there to learn about the basics of creating and working with an Android Activity. Once you have worked your way through them, read the application fundamentals to understand the lifecycle more fully.
You shouldn't be calling setContentView more than once in onCreate. The WebView should be in your main.xml layout file or else launch a new Activity that's layout contains the WebView. And also to hand the onClick on the button you need to call setOnClickListener().
Please see Handling UI Events.
Also, if your program crashes, it would be helpful if you provided error messages from logcat.