Xamarin Android - Setting ringtone from mp3 in asset folder - android

I am using Xamarin Forms and using Dependency Service to set ringtone from mp3 places in asset folder. Here is my code:
AssetManager assets = Forms.Context.Assets;
fileName = "Artist.mp3";
System.IO.Stream inputStream;
System.IO.Stream outputStream;
try
{
inputStream = assets.Open("Sounds/" + fileName);
**_ outputStream = Forms.Context.OpenFileOutput(fileName, FileCreationMode.Private); _**
byte[] buffer = new byte[65536 * 2];
int read;
while ((read = inputStream.Read(buffer, 0, (int)inputStream.Length)) != -1)
{
outputStream.Write(buffer, 0, read);
}
inputStream.Close();
inputStream = null;
outputStream.Flush();
outputStream.Close();
outputStream = null;
Java.IO.File newSoundFile = new Java.IO.File(basepath + "/Sounds/" + fileName);
if (newSoundFile.Exists())
{
ContentValues values = new ContentValues();
values.Put(MediaStore.MediaColumns.Data, newSoundFile.AbsolutePath);
values.Put(MediaStore.MediaColumns.Title, "Test Ringtone");
values.Put(MediaStore.MediaColumns.MimeType, "audio/*");
values.Put(MediaStore.MediaColumns.Size, newSoundFile.Length());
values.Put(MediaStore.Audio.Media.InterfaceConsts.Artist, "Artist MP3");
values.Put(MediaStore.Audio.Media.InterfaceConsts.Duration, 2300);
values.Put(MediaStore.Audio.Media.InterfaceConsts.IsRingtone, true);
values.Put(MediaStore.Audio.Media.InterfaceConsts.IsNotification, false);
values.Put(MediaStore.Audio.Media.InterfaceConsts.IsAlarm, false);
values.Put(MediaStore.Audio.Media.InterfaceConsts.IsMusic, false);
Android.Net.Uri uri = MediaStore.Audio.Media.GetContentUriForPath(newSoundFile.AbsolutePath);
Forms.Context.ContentResolver.Delete(uri, MediaStore.MediaColumns.Data + "=\"" + newSoundFile.AbsolutePath + "\"", null);
Android.Net.Uri newUri = Forms.Context.ContentResolver.Insert(uri, values);
try
{
RingtoneManager.SetActualDefaultRingtoneUri(Forms.Context, RingtoneType.Ringtone, newUri);
}
catch (Exception e)
{
}
}
}
catch (Exception e)
{
}
I am getting below exception in the line outputStream = Forms.Context.OpenFileOutput(fileName, FileCreationMode.Private)
System.NotSupportedException: Specified method is not supported.
at Android.Runtime.InputStreamInvoker.get_Length () [0x00000] in /Users/builder/data/lanes/3053/a94a03b5/source/monodroid/src/Mono.Android/src/Runtime/InputStreamInvoker.cs:55

You are implementing Stream.CopyTo manually. Just use it.
using (var inputStream = assets.Open("Sounds/" + fileName))
using (var outputStream = Forms.Context.OpenFileOutput(fileName, FileCreationMode.Private))
{
inputStream.CopyTo(outputStream);
}

Related

Trying to copy image one folder to another folder using contentResolver in android 11 but it create blank image

i m working on an application where i have list of images inside recyclerview and from recyclerview i have to copy my images from one folder to another folder my code is working fine in many android 11 and 12 device but in few device it creates blank images the size of blank image is in byte i don't understand what is wrong with my code
here is the code i used for copy image
public static String copyFile(#NonNull final String filePath, String newName, File targetFolder, Context context) {
ContentResolver contentResolver = context.getContentResolver();
FileOutputStream fos = null;
String folderName = targetFolder.getName();
String relativePath;
String fileName = new File(filePath).getName();
String mimeType = getFileType(fileName);
ArraySet<Uri> uries = new ArraySet<>();
if (isImage(mimeType)) {
uries.add(getImageUriFromFile(filePath, context));
} else {
uries.add(getVideoUriFromFile(filePath, context));
}
if (targetFolder.getPath().contains(Environment.DIRECTORY_PICTURES)) {
relativePath = Environment.DIRECTORY_PICTURES + File.separator + folderName;
} else if (targetFolder.getPath().contains(Environment.DIRECTORY_DCIM)) {
relativePath = Environment.DIRECTORY_DCIM + File.separator + folderName;
} else {
relativePath = Environment.DIRECTORY_PICTURES + File.separator + folderName;
}
try {
if (isImage(new File(filePath).getName().split("\\.")[1])) {
Bitmap bitmap = getBitmap(filePath);
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, newName);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/png");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativePath);
Uri imageUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
fos = (FileOutputStream) contentResolver.openOutputStream(imageUri);
if (!bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos)) {
fos.flush();
}
return getPath(imageUri, context);
} else {
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, newName);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "video/" + mimeType);
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativePath);
Uri imageUri = contentResolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentValues);
fos = (FileOutputStream) contentResolver.openOutputStream(imageUri);
FileInputStream inStream = new FileInputStream(filePath);
FileChannel inChannel = inStream.getChannel();
FileChannel outChannel = fos.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
inStream.close();
fos.close();
return getPath(imageUri, context);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
and i called this method inside thread like below.
i called this method from thread because i m doing another operation on copied file
new Thread(() -> StorageUtils.copyFileOnAboveQ(
file1.getPath(),
file2.getName().split("\\.")[0],
file2.getParentFile(), context)).start();
code is working fine in many devices but in some device it create blank image

How can I set a music from the software raw folder as the default alarm song?

I use the following code to save a music and select it as alarm music, but it does not work. where is the problem from?
(Files are saved but not selected as ringtone)
holder.btn_alarm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int resId = getMusicId(position, ringTonesDao);
File file = saveMusic(resId);
setForAlarm(file);
}
});
This code receives the music ID in the software.
public int getMusicId(int position, RingTonesDao ringTonesDao) {
long id = ringTonesList.get(position).getId();
String musicAddress = ringTonesDao.checkMusicAddress(id);
int resId = context.getResources().getIdentifier(musicAddress, "raw", context.getPackageName());
return resId;
}
This code, after receiving the music ID, saves it in the internal memory of the mobile and returns the saved file as the output of the method.
public File saveMusic(int resId) {
File dir;
String folderName = "Ringtones";
if (Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED)) {
dir = new File(Environment.getExternalStorageDirectory(), folderName);
} else {
dir = context.getCacheDir();
}
if (!dir.exists()) {
dir.mkdirs();
}
File file = new File(dir, resId + ".mp3");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
}
try {
InputStream inputStream = context.getResources().openRawResource(resId);
OutputStream outputStream = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
outputStream.flush();
outputStream.close();
inputStream.close();
} catch (Exception e) {
Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
}
}
return file;
}
This code receives the file and sets it as an alert song.
public void setForAlarm(File file) {
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, file.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, "alarm" + file.getName());
values.put(MediaStore.MediaColumns.SIZE, file.length());
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
values.put(MediaStore.Audio.Media.DURATION, 230);
values.put(MediaStore.Audio.Media.IS_RINGTONE, false);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, false);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(file.getAbsolutePath());
Uri newUri = context.getContentResolver().insert(uri, values);
String[] columns = {MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.IS_ALARM
};
Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, columns, MediaStore.Audio.Media.DATA + " = '" + file.getAbsolutePath() + "'", null, null);
if (cursor != null) {
int idColumn = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
int fileColumn = cursor.getColumnIndex(MediaStore.Audio.Media.DATA);
int ringtoneColumn = cursor.getColumnIndex(MediaStore.Audio.Media.IS_ALARM);
while (cursor.moveToNext()) {
String audioFilePath = cursor.getString(fileColumn);
if (cursor.getString(ringtoneColumn) != null && cursor.getString(ringtoneColumn).equals("1")) {
Uri hasUri = MediaStore.Audio.Media.getContentUriForPath(audioFilePath);
newUri = Uri.withAppendedPath(hasUri, cursor.getString(idColumn));
}
}
cursor.close();
}
RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_ALARM, newUri);
}

Download video in Downloads folder Android Q

As per the restrictions of Android Q and above, I am trying to download the video file in Downloads folder under folder. But It always takes to the Android/data/<package_name>/Downloads/ folder. minSdk for my app is API 16, and currently targetSdk is API 29.
Below is reference code snippet used.
String videoFileName = "video_" + System.currentTimeMillis() + ".mp4";
ContentValues valuesvideos;
valuesvideos = new ContentValues();
valuesvideos.put(MediaStore.Video.Media.RELATIVE_PATH, "Movies/" + "Folder");
valuesvideos.put(MediaStore.Video.Media.TITLE, videoFileName);
valuesvideos.put(MediaStore.Video.Media.DISPLAY_NAME, videoFileName);
valuesvideos.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
valuesvideos.put(MediaStore.Video.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
valuesvideos.put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis());
valuesvideos.put(MediaStore.Video.Media.IS_PENDING, 1);
ContentResolver resolver = mContext.getContentResolver();
Uri collection = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
Uri uriSavedVideo = resolver.insert(collection, valuesvideos);
ParcelFileDescriptor pfd;
try {
pfd = mContext.getContentResolver().openFileDescriptor(uriSavedVideo, "w");
FileOutputStream out = new FileOutputStream(pfd.getFileDescriptor());
File storageDir = new File(mContext.getExternalFilesDir(Environment.DIRECTORY_MOVIES), "Folder");
File imageFile = new File(storageDir, "Myvideo");
FileInputStream in = new FileInputStream(imageFile);
byte[] buf = new byte[8192];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.close();
in.close();
pfd.close();
} catch (Exception e) {
e.printStackTrace();
}
valuesvideos.clear();
valuesvideos.put(MediaStore.Video.Media.IS_PENDING, 0);
mContext.getContentResolver().update(uriSavedVideo, valuesvideos, null, null);
Thank You in Advance.

How to save audio file from raw folder to phone

Below I have the coding for hiow to set audio as your ringtone. This works fine however I wanna know how could I alter this such that it well save just the audio so that it can be played with my other songs
public boolean save1(int type) {
byte[] buffer = null;
InputStream fIn = getBaseContext().getResources().openRawResource(
R.raw.song);
int size = 0;
try {
size = fIn.available();
buffer = new byte[size];
fIn.read(buffer);
fIn.close();
} catch (IOException e) {
return false;
}
String path = Environment.getExternalStorageDirectory().getPath()
+ "/media/audio/";
String filename = "New song";
FileOutputStream save;
try {
save = new FileOutputStream(path + filename);
save.write(buffer);
save.flush();
save.close();
} catch (FileNotFoundException e) {
return false;
} catch (IOException e) {
return false;
}
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse("file://" + path + filename)));
File k = new File(path, filename);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, k.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, filename);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/ogg");
if (RingtoneManager.TYPE_RINGTONE == type) {
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
} else if (RingtoneManager.TYPE_NOTIFICATION == type) {
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
} else if (RingtoneManager.TYPE_ALARM == type) {
values.put(MediaStore.Audio.Media.IS_ALARM, true);
}
Uri uri = MediaStore.Audio.Media.getContentUriForPath(k
.getAbsolutePath());
Uri newUri = MainActivity.this.getContentResolver().insert(uri, values);
RingtoneManager.setActualDefaultRingtoneUri(MainActivity.this, type,
newUri);
this.getContentResolver()
.insert(MediaStore.Audio.Media.getContentUriForPath(k
.getAbsolutePath()), values);
return true;
}
To move raw folder file to sdcard or phone storage below code will work:
private void CopyRAWtoPhone(int id, String path) throws IOException {
InputStream in = getResources().openRawResource(id);
FileOutputStream out = new FileOutputStream(path);
byte[] buff = new byte[1024];
int read = 0;
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();
out.close();
}
}
where first Variable id will be your raw file id and String path will be your phone storage path.

How to set custom Alarm tone in android

I need to set custom alarm tone in my App. Could anyone please just tell me how to set custom ringtone or Mp3 as an alarm ? Any kind of help will be appreciated.
Here is also a solution for this problem
setting audio file as Ringtone
Best,
Shahzad Majeed
You can use audio player to play your mp3.But here is a better alarm app which fulfills your requirements.
http://code.google.com/p/kraigsandroid/source/browse/#git%2Fandroid%2Falarmclock%2Fsrc%2Fcom%2Fangrydoughnuts%2Fandroid%2Falarmclock
in the Android docs look at the Status Bar Notifications page. In particular see the Adding a Sound section.
Try this
add any .mp3 file in raw folder place name of that file
public void setAlarm() {
File file = new File(Environment.getExternalStorageDirectory(),
"/Your Directory Name");
if (!file.exists()) {
file.mkdirs();
}
String path = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/Your Directory Name";
File f = new File(path + "/", filename + ".mp3");
Uri mUri = Uri.parse("android.resource://" + getContext().getPackageName() + "/raw/" + filename);
ContentResolver mCr = getContext().getContentResolver();
AssetFileDescriptor soundFile;
try {
soundFile = mCr.openAssetFileDescriptor(mUri, "r");
} catch (FileNotFoundException e) {
soundFile = null;
}
try {
byte[] readData = new byte[1024];
FileInputStream fis = soundFile.createInputStream();
FileOutputStream fos = new FileOutputStream(f);
int i = fis.read(readData);
while (i != -1) {
fos.write(readData, 0, i);
i = fis.read(readData);
}
fos.close();
} catch (IOException io) {
}
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, f.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, filename);
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.MediaColumns.SIZE, f.length());
values.put(MediaStore.Audio.Media.ARTIST, R.string.app_name);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(f.getAbsolutePath());
getContext().getContentResolver().delete(uri, MediaStore.MediaColumns.DATA + "=\"" + f.getAbsolutePath() + "\"", null);
Uri newUri = mCr.insert(uri, values);
try {
RingtoneManager.setActualDefaultRingtoneUri(getContext(),
RingtoneManager.TYPE_ALARM, newUri);
Settings.System.putString(mCr, Settings.System.ALARM_ALERT,
newUri.toString());
Toast.makeText(getContext(), "Done", Toast.LENGTH_SHORT).show();
} catch (Throwable t) {
}
}

Categories

Resources