I try the following code and it doesn't set the ringtone. The logcat entry for "ff" says null so I guess the URI isnt being concatenated properly?, I cant seem to figure out where in my code I am going wrong.
String filepath =Environment.getExternalStorageDirectory().toString()+"//media//audio//ringtones//bluemoon.mp3";
File ringtoneFile = new File(filepath);
ContentValues content = new ContentValues();
content.put(MediaStore.MediaColumns.DATA,ringtoneFile.getAbsolutePath());
content.put(MediaStore.MediaColumns.TITLE, "test");
content.put(MediaStore.MediaColumns.SIZE, 215454);
content.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mpeg");
content.put(MediaStore.Audio.Media.ARTIST, "artist");
content.put(MediaStore.Audio.Media.DURATION, 230);
content.put(MediaStore.Audio.Media.IS_RINGTONE, true);
content.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
content.put(MediaStore.Audio.Media.IS_ALARM, false);
content.put(MediaStore.Audio.Media.IS_MUSIC, false);
Log.i("BOOM", "the absolute path of the file is :"+ringtoneFile.getAbsolutePath());
Uri uri = MediaStore.Audio.Media.getContentUriForPath(
ringtoneFile.getAbsolutePath());
Uri newUri = getApplicationContext().getContentResolver().insert(uri, content);
Uri ringtoneUri = newUri;
Log.i("ff","the ringtone uri is :"+ringtoneUri);
RingtoneManager.setActualDefaultRingtoneUri(getApplicationContext(),
RingtoneManager.TYPE_RINGTONE,newUri);
Its possible data is not accurate for the file and its causing a problem. You may need to change:
audio/mpeg to audio/mp3
and duration to the actual duration of the file.
Another thing you may want to fix is your ringtone file declaration statement, try changing it to match this:
File k = new File(path, "mysong.mp3");
Also, I have a feeling your path is broken too..
Environment.getExternalStorageDirectory().toString()+"//media//audio//ringtones//bluemoon.mp3"
why with the two "//"?
It should be (for example):
../sdcard/media/ringtone
Related
I'm developing a test with Espresso to test the profile image change function. I added the following lines in the #Before method in the test.
I create an intent with the image Uri, with my file provider, to return ever that my app goes to the gallery to pick an image.
Intent resultData = new Intent();
String filename = "img1.jpg";
String path = "mnt/sdcard/" + filename;
File f = new File(path);
Context context =InstrumentationRegistry.getInstrumentation().getContext();
Uri contentUri = getUriForFile(context, "com.otsuka.ikigai.fileprovider", f);
resultData.setData(contentUri);
Instrumentation.ActivityResult result = new Instrumentation.ActivityResult(Activity.RESULT_OK,resultData);
intending(not(isInternal())).respondWith(result);
The code of the activity that changes user image, calls the following method when it receives the intent,(I must not change it).
mProfileImage = CommonBitmapUtils.rotate(this, data.getData());
profileEdited = true;
imgUserPhoto.setImageBitmap(mProfileImage);
And I'm getting the following error:
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
Caused by this line in the function rotate of the CommonBitmapUtils class:
path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
The cursor has 0 rows don't know why.
Solved it by setting the following path. Without file providers and permissions.
I got the path by debugging the app without testing, and copying the value.
resultData.setData(Uri.parse("content://media/external/images/media/142583"));
I have a programmed a custom exoplayer videoview to play videos from both internal memory and URL's. Now i want to check whether the video that is played presently in Videoview is from URL Stream or Internal storage. How can i check this?
You can use two methods to determine whether it's internal or remote.
Method 1 - Using Uri
Parse the given URI using Uri.parse(...) then use getScheme() to get the scheme. For (internal) files it will be file.
For example:
String file = "file:///path/of/your/file";
String remote = "https://www.example.com/your_video.mp4";
Uri fileUri = Uri.parse(file);
Uri remoteUri = Uri.parse(remote);
String fileScheme = fileUri.getScheme();
String remoteScheme = remoteUri.getScheme();
Log.d("Scheme", fileScheme); // prints file
Log.d("Scheme", remoteScheme); // prints https
Method 2 - Using File
Create a new File instance from the URI and check if it exists.
For example:
String file = "file:///path/of/your/file";
String remote = "https://www.example.com/your_video.mp4";
File fileFile = new File(file);
File remoteFile = new File(remote);
Log.d("File", "" + fileFile.exists()); // prints true if the file exists
Log.d("File", "" + remoteFile.exists()); // prints false always
I have one java.io.File pointed to /storage/files/pic.jpg (for brevity) called mFile
When I try to query for the file using
Uri photoUri = Uri.fromFile(mFile);
Cursor cursor = context.getContentResolver().query(photoUri,
new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);
cursor ends up null, would expect cursor not not be null.
photoUri.to_String returns file:///storage/files/pic.jpg
I know the file exists, since i can load it with
BitmapFactory.decodeFile(mFile.getAbsolutePath());
or open an InputStream with the same Uri
InputStream is = context.getContentResolver().openInputStream(photoUri);
I get the feeling that photoUri is malformed somehow, I have seen posts saying it should be content://..., but they also refers to obsolete versions of the android sdk.
I need a quick help on how to convert android Uri to Java URI. My requirement is to capture images and store them in External storage. To pass these images across activities, I decided to use an Arraylist that holds Uris of images and pass on this arraylist as an intent-extra to next activity. But, Arraylist accepts only JavaURI.
String image = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
File photo=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),image);
selectedImageA=Uri.fromFile(photo);
imageIndex++;
selectedImageJ= <Looking for code here>;
imagesUriArray.add(imageIndex,selectedImageJ);
Intent i = new Intent(getApplicationContext(),ScanActivity.class);
startActivity(i);
Try to declare:
ArrayList<Uri> imagesUriArray = new ArrayList<Uri>;
If this doesn't work, declare your ArrayList as String list and convert the uri to String. And later, initialize the uri from the Uri string
I am new to java.
I have a directory with a txt file (R.raw). I want to get access with a command
InputStream in_s = res.openRawResource(R.raw.itemname);
where itemname is a dynamic string with a filename from a previous activity.
How do I can get open file in R.raw by a string "n0.txt"?
In javascript I can implement it like R.raw["n0.txt"] or R.raw.itemname.
Thanks in advance.
You need to use method getIdentifier().
Context context;
Resources res;
int itemId = res.getIdentifier("itemname", "raw", context.getPackageName());
InputStream is = res.openRawResource(itemId);
Original post here.