Android videoview - Error for internal storage URI - java

I am a beginner at Android programming and I had a doubt to be clarified.
I tried out a tutorial on VideoView in Android and observed that,
When the specified URI string is "http://www.androidbegin.com/tutorial/AndroidCommercial.3gp", the program works.
I tried replacing the URI string with the location of a video present in the phone's internal storage (/storage/emulated/0/Movies/test.mp4) and the program produced the error java.io.IOException: setDataSource failed.
My question is what does the error signify and why does it occur ? since both the URI string's do specify the video to be played.
(Note: I followed this tutorial)

Try this code to get a video from gallery:
// in onCreate method
Intent getVid= new Intent();
getVid.setType("video/*");
getVid.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(getVid, "Select a video" ),
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
} if (requestCode == 1 && resultCode == RESULT_OK){
String videoUrl = data.getData().toString();
Intent i = new Intent(MainActivity.this , videoViewActivity.class);
i.putExtra("vid" , videoUrl);
startActivity(i);
}
in videoViewActivity type this code in onCreate method after initializing videoView :
String path = getIntent().getStringExtra("vid");
videoView.setVideoPath(path);
videoView.start();

It's probably because of your Uri. When you use Uri.parse("/blabla") it's not validating that path, is it really exist or not. And in your case you need to give something like "file:///storage/emulated/0/Movies/test.mp4". Or your app don't have file permission, you need to add a permission check first.

Related

How I can get image Uri from gallery for a long time?

I'm a junior in android development and I faced a problem with getting Uri for a long time. My aim is get an Image Uri and show the image after few days. I use this method and it works when I restart my app, but in doesn't work when I try to upload image using the same Uri after a day. What i should do to get long Uri?
Intent galleryIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, 1);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1 && resultCode == RESULT_OK){
Uri uri = data.getData();
save(uri.toString());
}
}
because of this URL refers to a local image and I think will not work if you removed this image or renamed it,
you can transform it to base64 and save it in shared preferences or local DB.

Open file from documents on Android Nougat

I'm using file helper class from this post https://stackoverflow.com/a/20559175/2281821 but it doesn't working properly in each case. I'm using Intent chooseIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT); to start file picker. I have Android Nougat and I'm receiving from onActivityResult uri: content://com.android.externalstorage.documents/document/home%3Aimage.png
What does this home: means? How can I get access to this file? I was trying with Environment.getExternalStorageDirectory() and System.getenv("EXTERNAL_STORAGE") but I can't get acess.
Following code can be used to get document path:
Intent galleryIntent = new Intent();
galleryIntent .setType("image/*");
galleryIntent .setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(galleryIntent , getString(R.string.app_name)),REQUEST_CODE);
OnActivityResult():
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CODE) {
Uri uri = data.getData();
}
}
}
You can use "uri" variable value to get bitmap. If you want to open document other image also then remove following line:
galleryIntent .setType("image/*");
You get access to a content scheme by opening for instance an InputStream on the uri.
InputStream is = getContentResolver().openInputStream(uri);
BitmapFactory.decodeFromStream(is) will happily read from your stream.

File upload with Android 4.4.4 in webview

I am trying to upload images via a WebView in Android. The problem is the missing content type. Seems like it`s a known issue in Android 4.4.4. What can be done in this situation? I found this answer on similar question, but I can't figure out how to implement this solution. I have access to server side.
Thanks.
In the answer you found, they call
startActivityForResult( Intent.createChooser( i, "File Chooser" ), MainActivity.FILECHOOSER_RESULTCODE)
What means, you should get the results of this in this method
protected void onActivityResult( int requestCode, int resultCode, Intent data )
{
if(requestCode == MainActivity.FILECHOOSER_RESULTCODE)
{
if(resultCode == RESULT_OK)
// TODO: Check Results of data-intent
}
}
in this method you can handle the results from the file chooser and do a upload by yourself (e.g. with URLConnection or ApacheHttpClient).
UPDATE 2016-10-19
Here is a Example where the ValueCallback is stored and the result of the ChooserIntent is passed back to the callback.
I didn`t try this example, but I think it should trigger a own upload methode from the webview.
private ValueCallback<Uri> mUploadMessage;
private Uri mCapturedImageURI = null;
protected void onActivityResult( int requestCode, int resultCode, Intent data )
{
if(requestCode == MainActivity.FILECHOOSER_RESULTCODE)
{
if(resultCode == RESULT_OK) {
result = intent == null ? mCapturedImageURI : intent.getData();
mUploadMessage.onReceiveValue(result);
}
}
}
onReceiveValue(result);
Source: http://androidexample.com/Open_File_Chooser_With_Camera_Option_In_Webview_File_Option/index.php?view=article_discription&aid=128
Check this thread for more examples https://stackoverflow.com/a/7857102/2377961

file chooser for android

I am making an android album app where I can create an album and add photos and delete photos from the album. Adding a photo is a bit tricky where I need a photo filename with a file path. This was very easy using JFileChooser in java but this is android and I have no clue on getting the filename and file path. Is there any thing in the android api where I can get the same functionality as the JFileChooser.
I am looking for a solution to this problem either using a file chooser of some sort or an entire to new approach. Any help is appreciated..
Or is there any other approach I can implement to add a photo...
You may use Intent.ACTION_PICK to invoke an image picker. This intent may be caught by the default gallery app, or some other app installed on the device.
private static final int REQUEST_PICKER = 1;
private void invokePicker() {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Complete action using"), REQUEST_PICKER);
}
Then receive the result on onActivityResult.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK)
return;
if (requestCode == PICK_FROM_FILE) {
// Get Uri of the file selected,
Uri theImageUri = data.getData();
// Or if you want a Bitmap,
Bitmap theBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), theImageUri);
}
}
Edited:
Though in this way you don't need a real file path, you can get it from MediaStore if you need.

How to check contents of History in android?

I am running my own application on "Samsung Y" which launches ZXing when triggers button,I don't know how to see details of barcode scanned.
Please someone help me
when you want to call ZXing you put this
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
startActivityForResult(intent, 0);
Then you make an onActivityResult to capture the result from ZXing
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
// The actual code result
String contents = intent.getStringExtra("SCAN_RESULT");
// Type of barcode scanned (Barcode, QR, etc.)
String format = intent.getStringExtra("SCAN_RESULT_FORMAT"); //
}
}
You could also use their intentIntegrator
There are many similar questions. Check my answer here: How to use Zxing in android if u want to save history intent.putExtra("SAVE_HISTORY",true); instead of false

Categories