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
Related
I've been trying to follow Android loadThumbnail Album Artwork (API 29), but I always get the same error that the OP had in that thread, even when using the fixed code given in the answer. Songs can play normally, it's just the album art that gives
java.io.FileNotFoundException: No media for album content://media/external/audio/albums/*number*
This is my current code, if I use MediaStore.Audio.Media.ALBUM_ID instead of MediaStore.Audio.Albums._ID, the app crashes entirely. Anyone know where I went wrong?
String[] projection = {
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Albums._ID
};
Cursor cursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection, null, null, null);
while (cursor.moveToNext()) {
long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Albums._ID));
Bitmap albumArt = null;
try {
albumArt = getAlbumArtwork(getContentResolver(), id);
} catch (IOException e) {
e.printStackTrace();
}
private Bitmap getAlbumArtwork(ContentResolver resolver, long albumId) throws IOException {
Uri contentUri = ContentUris.withAppendedId(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, albumId);
return resolver.loadThumbnail(contentUri, new Size(300, 300), null);
}
I am looking to read a set of image files from the sdcard/download directory. I am getting content://media/external_primary/downloads as the URI string when calling
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
collection = MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
} else {
collection = MediaStore.Downloads.EXTERNAL_CONTENT_URI;
}
I am using the Cursor interface to iterate through the directory but for some reason, my cursor is returning with a size of 0. I do not know what I am doing wrong to make this happen but below is the code that I have so far. Looking for some direction.
Uri collection;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
collection = MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
} else {
collection = MediaStore.Downloads.EXTERNAL_CONTENT_URI;
}
String[] projection = {
MediaStore.MediaColumns._ID,
MediaStore.MediaColumns.TITLE
};
try {
Cursor cursor = getApplicationContext().getContentResolver().query(collection, projection, null, null, null);
// Cache column indices.
int idColumn = cursor.getColumnIndexOrThrow(MediaStore.Downloads._ID);
int nameColumn = cursor.getColumnIndexOrThrow(MediaStore.Downloads.TITLE);
while (cursor.moveToNext()) {
// Get values of columns for a given video.
long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Downloads._ID));
String name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Downloads.DISPLAY_NAME));
Uri contentUri = ContentUris.withAppendedId(MediaStore.Downloads.EXTERNAL_CONTENT_URI, id);
ImageView imageView = findViewById(R.id.imageView);
imageView.setImageURI(contentUri);
}
} catch (Exception e) {
e.printStackTrace();
}
Snippet to the directory structure
https://i.stack.imgur.com/ELkQw.png
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?
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..
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);