Motorola Android 2.2 camera ignore EXTRA_OUTPUT parameter - java

I programatically open camera to take a video. I tell camera to put the video file to a specified place using code like below:
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
File out = new File("/sdcard/camera.mp4");
Uri uri = Uri.fromFile(out);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, GlobalUtility.CAMERA_VIDEO);
It works well on a HTC phone. But on my moto defy, it just ignore the MediaStore.EXTRA_OUTPUT parameter, and put the video to the default place.
So then I use this code in onActivityResult() function to solve the problem:
private String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
String realPath;
try {
File file = new File("/sdcard/camera.mp4");
if (!file.exists()) {
Uri videoUri = data.getData();
realPath = getRealPathFromURI(videoUri);
}
} catch (Exception ex) {
Uri videoUri = data.getData();
realPath = getRealPathFromURI(videoUri);
}
Hope this will help some others.

Just because /sdcard/ is the sdcard directory on one phone and one build of Android doesn't mean that will stay consistent.
You will want to use Environment.getExternalStorageDirectory() as Frankenstein's comment suggests. This will always work to get the directory of the SD Card.
You will also want to check that the SD Card is currently mountable as the phone may be in USB Storage mode.
Try something like...
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Log.d(TAG, "No SDCARD");
} else {
File out = new File(Environment.getExternalStorageDirectory()+File.separator+"camera.mp4");
}

I have done this way and still didn't found any error..so please try this in you "moto defy" so I can know the reality.
To Call Intent :
Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent,2323);
In Activity on Result:
Uri contentUri = data.getData();
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String tmppath = cursor.getString(column_index);
videoView.setVideoPath(path);

Related

how to get the file path from the file chooser after selecting the file from the file storage

I am trying to create a personal chatting app which has a button. When click of that button the frame becomes visible which have buttons on click chooser is been created like this. When the image button is clicked it creates the chooser activity after selecting the file I have saved it in imagefile Uri.
But when I try to get the path of the file by using the data.getdata().getpath() method it gives doument/236 as the output but didn't give the actual path of the file. When I try to use fileutlis to get the path then it says "can't resolve fileutils". Please help me so that I can get the path of my file.
imagesend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
checker = "image";
Intent imageIntent = new Intent();
imageIntent.setAction(Intent.ACTION_GET_CONTENT);
imageIntent.setType("image/*");
startActivityForResult(Intent.createChooser(imageIntent,"Select Image"),438);
}
});
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 438 && resultCode == RESULT_OK && data!=null && data.getData()!=null){
loadingBar.setTitle("Sending Message");
loadingBar.setMessage("Please wait...");
loadingBar.setCanceledOnTouchOutside(false);
loadingBar.show();
imagefile = data.getData();
String filepath = data.getData().getPath();
if(checker.equals("pdf")){
pdfFilemessage();
}else if(checker.equals("image")){
//imagefilemessage();
Toast.makeText(personalChat.this,filepath,Toast.LENGTH_SHORT).show();
}
}
}
This line returns the Uri of the file.
imagefile = data.getData();
What you have to do is,
public String getRealPathFromURI(Uri contentUri) {
String[] proj = {
MediaStore.Audio.Media.DATA
};
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
Add this to your code.
String filePath = this.getRealPathFromURI(imagefile);
Refer this: https://stackoverflow.com/a/13209514
You can try this:
Uri fileUri = intent.getData();
Now from this you can use the below gist file to generate the file path from the URI
https://gist.github.com/tatocaster/32aad15f6e0c50311626
With Android 10 you won't by default be able to access the file directly by path as Google are forcing people to use Storage Access Framework and or MediaStore to access files.
With Android 11 their plan is to make this the only way to access files.
See https://developer.android.com/training/data-storage/files/external-scoped for details.
You will need to use ContentResolver#openFileDescriptor(Uri, String)
and ParcelFileDescriptor#getFileDescriptor() to get something usable
e.g.
ParcelFileDescriptor pfd =
this.getContentResolver().
openFileDescriptor(Uri, "r");
FileInputStream fileInputStream =
new FileInputStream(
pfd.getFileDescriptor());
If you want to get other info about the file you can do that with a contentResolver Query on the URI for DISPLAY_NAME or MIME_TYPE for type of file.
e.g.
// useful name to display to user
Cursor cursor = this.getContentResolver()
.query(Uri, new String[] { MediaStore.Files.FileColumns.DISPLAY_NAME},
null, null, null);
cursor.moveToFirst();
filename = cursor.getString(0);
cursor.close();

how to get information of music files from assets folders?

I have following code which reads my phone and gets information of the music files in my phone
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
String[] projectionSongs = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DURATION};
Cursor cursor = mContext.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projectionSongs,
selection, null, null);
Now I want to same information from assets folder itself.
I am trying
Uri uri = Uri.parse("file:///assets/");
Cursor cursor = mContext.getContentResolver().query(uri, projectionSongs,
selection, null, null);
But it is not working. There is no information in cursor. However I have some mp3 files in assets folder.
public void playBeep() {
try {
if (m.isPlaying()) {
m.stop();
m.release();
m = new MediaPlayer();
}
AssetFileDescriptor descriptor = getAssets().openFd("your_audio_file_name.mp3");
m.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
m.prepare();
m.setVolume(1f, 1f);
m.setLooping(true);
m.start();
} catch (Exception e) {
e.printStackTrace();
}
}
Hope this will help u. Thank you

How to get a list of all audio files using android.database.Cursor?

I came across the solution of this problem through Cursor,
public class MainActivity extends AppCompatActivity {
void example() {
List<File> localAudioFiles = new LinkedList<>();
Uri contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.Images.Media.DATA};
// The problem is that it contains a list of files as of the moment
// when the operating system has been rebooted.
try (Cursor cursor = getContentResolver().query(
contentUri,
projection,
MediaStore.Audio.Media.IS_MUSIC + " != 0",
null,
null);) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
while (cursor.moveToNext()) {
String audio = cursor.getString(column_index);
localAudioFiles.add(new File(audio));
}
} catch (RuntimeException ex) {
}
}
}
but as far as I understand, its state needs to be updated compulsorily, because files added through the AVD file manager are not detected in this way until the device is rebooted.
How can I forcefully update the state of the entity Cursor?
Is the solution worth it, or is it not better than recursively traversing all the directories?

Android Intent ACTION_GET_CONTENT does not return extension of file

I am trying get the path of the file selected by user, using calling the intent ACTION_GET_CONTENT for result.
The problem is when Selecting an audio file from the file manager, the intent does not return the extension of the file (which is there in the file name i checked it). In the case of video or image it is working fine.
Here is the code:
Intent Calling:
Intent intent = getIntent();
if(intent.getAction() == null) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
else
intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"),CloudConstants.CLOUD_REQUEST_FILE_CHOOSER);
On Result Code:
if (data != null) {
//Get URI Data from Intent - URI is of the file chosen by the User in the
//File picker
uriFileURI = data.getData();
if(uriFileURI != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
final int intFlags = data.getFlags()&(Intent.FLAG_GRANT_READ_URI_PERMISSION|Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
getContentResolver().takePersistableUriPermission(data.getData(), intFlags);
}
//Check if URI was returned or not; NULL is returned if file was chosen from
//via gallery share option
//In such a case, the URI is retrieved from ClipData object of the Intent
if (uriFileURI == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN && data.getClipData().getItemCount() > 0)
uriFileURI = data.getClipData().getItemAt(0).getUri();
//Log File URI
Log.i("CloudMedia", "File Uri: " + String.valueOf(uriFileURI));
//Generate Absolute File name to publish on Title
strFileName = getFileInformation(uriFileURI, MediaStore.Files.FileColumns.DISPLAY_NAME);
getFileInformation Function:
public String getFileInformation(Uri strFileURI, String strProjection) {
Cursor cursorFileId = getContentResolver().query(strFileURI,
new String[] {
strProjection
}, null, null, null);
if(cursorFileId.moveToFirst()) {
return cursorFileId.getString(cursorFileId.getColumnIndex(strProjection));
} else
return null;
}
So the strFileName does not contain the extension of the audio file selected.
I want the audio file extension also.
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String imgFullPath = cursor.getString(columnIndex);
cursor.close();//String file = uri.toString();
source http://programmerguru.com/android-tutorial/how-to-pick-image-from-gallery/
Try this code. I used android.provider.OpenableColumns.DISPLAY_NAME instead of MediaStore.Files.FileColumns.DISPLAY_NAME and set null for projection in getContentResolver().query
strFileName = getFileInformation(uriFileURI, android.provider.OpenableColumns.DISPLAY_NAME);
public String getFileInformation(Uri strFileURI, String strProjection) {
Cursor cursorFileId = getContentResolver().query(strFileURI,
null, null, null, null);
if(cursorFileId.moveToFirst()) {
return cursorFileId.getString(cursorFileId.getColumnIndex(strProjection));
} else
return null;
}

Android get image Uri from camera, store in SQLite and restore it on my custom adapter

I followed the Android developers example of get an image form the camera intent and place it on your view. The problem starts when I try to save that image uri on my SqliteDatabase ( just the link to that image not the full image so I save space) and then I try to restore it on my customadapter.
Link to google dev - > http://developer.android.com/training/camera/index.html
I tried this without success
created a global string logo, and inside handleSmallCameraPhoto put this
private void handleSmallCameraPhoto(Intent intent) {
Bundle extras = intent.getExtras();
mImageBitmap = (Bitmap) extras.get("data");
ImagenViaje.setImageBitmap(mImageBitmap);
ImagenViaje.setVisibility(View.VISIBLE);
--> logo = extras.get("data").toString();
Then I stored logo on SQLite, and tried to restore it on my adapter this way
String imagePath = c.getString(c.getColumnIndexOrThrow(MySQLiteHelper.COLUMN_LOGO_PATH));
Then
ImageView item_image = (ImageView) v.findViewById(R.id.logo);
item_image.setVisibility(ImageView.INVISIBLE);
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
item_image.setImageBitmap(bitmap);
item_image.setVisibility(ImageView.VISIBLE);
get path of image using
Uri selectedImageUri = intent.getData();
String s1 = intent.getDataString();
String selectedImagePath = getPath(selectedImageUri);
if(selectedImagePath==null && s1 != null)
{
selectedImagePath = s1.replaceAll("file://","");
}
in your handleSmallCameraPhoto method
add this method in your activity
public String getPath(Uri uri) {
try{
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
if(cursor==null)
{
return null;
}
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
catch(Exception e)
{
return null;
}
}
save selectedImagePath in your database and use when you need selectedImagePath is path of your selected image
hope help you..

Categories

Resources