This is Success.
notificationBuilder.setSound(Uri.parse("android.resource://" + context.getPackageName() + "/raw/testfile"));
However, in the following way is Not Work.
notificationBuilder.setSound(Uri.parse("file:///data/data/com.Company.test/files/testfile.wav"));
File outFile = new File("/data/data/com.Company.test/files/testfile.wav");
outFile.exists() == true
File exists.
Why ?
Put your file in raw folder
and give uri path
Uri path = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.testfile);
Related
I am trying to add data into a folder which is inside Android/Data/packagename. So I am trying this out:
String tempsubdirtest = Environment.getExternalStorageDirectory()
+ File.separator + "/Android/data/" + getPackageName() + "/files/Images";
File subdirecttest = new File(tempsubdirtest);
if (!subdirecttest.exists())
{
subdirecttest.mkdirs();
}
The question is that is there an easy way to reduce the code for:
Environment.getExternalStorageDirectory()
+ File.separator + "/Android/data/" + getPackageName()
instead of me typing /Android/data/ +getPackName etc.
Thanks!
You can use this existing method getExternalCacheDir
This will return the directory path plus a cache folder.
So in your case, you can just exclude that cache path.
/storage/emulated/0/Android/data/com.example.test/cache
You might gonna check this, which will return something similar to what you want.
String pathImage = getExternalFilesDir(Environment.DIRECTORY_PICTURES).getPath();
/storage/emulated/0/Android/data/com.example.test/files/Pictures
I am able create and start an intent to capture a video. But how can I set the file name and save it to a specific directory?
Here's what I have so far:
videoPath = "/X1," + num1 +
",Y1," + num2 +
",X2," + num3 +
",Y2," + num4 +
",A," + num5 +
",G," + num6 +
",la," + num7 +
",lo," + num8+ ".mp4";
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, videoPath);
startActivityForResult(intent, VIDEO_INTENT);
But how can I set the file name and save it to a specific directory?
You are setting the filename. It is most of what you have in videoPath.
However:
Your videoPath does not specify a directory, other than an invalid leading slash. Use getExternalFilesDir() on Context to get a likely File object for the directory, then create a new File object pointing to the actual file you want to use.
I am not sure if commas will work well in filenames here.
EXTRA_OUTPUT is a Uri, not a String or a File. Use Uri.fromFile() to convert your File to a Uri.
Tested in Emulator
At first I make a directory in this form :
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File(root.getAbsolutePath() + "/" + "SubFolderName");
if (!dir.exists()) {
dir.mkdirs();
That make directory in this path : /storage/emulated/0/SubFolderName
Now, I need to use this path for downloaded file, so add this code:
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDestinationInExternalPublicDir(root.getAbsolutePath() + "/" + "SubFolderName","FileName");
That return this path: /storage/emulated/0/storage/emulated/0/SubFolderName/
Too try before this:
request.setDestinationInExternalFilesDir(getActivity(), root.getAbsolutePath() + "/" + "SubFolderName","FileName");
This section are repeated : storage/emulated/0
I check my codes but there-are not mistake.
You are not specifying which directory in external storage.
There are several directories you can use depending on what you are storing e.g "Downloads", "Video"
You can use the downloads directory like this for example:
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).mkdirs();
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS + "/" + "SubFolderName/" + mTedListModel.get(position).getTitle()).mkdirs();
And then set your destination with the request manager as
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, + "SubFolderName/" + mTedListModel.get(position).getTitle());
You are using setDestinationInExternalPublicDir(), as i can read here it is already adding the external root dir so you only need to especify the directory type,subpath and file name.
Example:
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOCUMENTS,"SubFolderName/FileName");
Hope im not wrong.
i have a strange behavior while coding a video player. I have an html5 menu that targets mp4 videos. When you click on a video, the path will be treated and a native video player intent will start to play the video. My videos are placed in "raw" folder.
When I use a static path, the video plays very well:
String uriPath = "android.resource://" + getPackageName() + "/" + R.raw.video1;
When I use the following path, I can't play it:
video_title = getIntent().getExtras().getString("video_title");
String uriPath = "android.resource://" + getPackageName() + "/" + "R.raw."+video_title;
Note that i removed the extension of the file in the main intent so the variable "video_title" will hold the video title without extension.
I resolved this issue, by changing the uriPath variable:
String uriPath = "android.resource://" + getPackageName() + "/" + "R.raw."+video_title; // BAD
String uriPath = "android.resource://" + getPackageName() + "/" + "raw/"+video_title; // GOOD
I'm trying to access the image on the SD card using below code but I"m getting File not found exception. Can someone tell me a correct way to acess the file???
new File("/mnt/sdcard/splah2.jpg")
Try like this:
String SD_CARD_PATH = Environment.getExternalStorageDirectory().toString();
new File(SD_CARD_PATH + "/" + "splah2.jpg");
Try running: new File(Environment.getExternalStorageDirectory() + "/splah2.jpg")
try this,
File f=new File("/sdcard/splah2.jpg");
The code below has worked for me.
String mRelativeFolderPath = "/DCIM/Camera/"; // i.e. SDCard/DCIM/Camera
String mBaseFolderPath = Environment.getExternalStorageDirectory().getAbsolutePath() + mRelativeFolderPath;
String filePath = mBaseFolderPath + "test.jpg";
File handle = new File(filePath);
Shash
try
new File("/sdcard/splah2.jpg")
or
new File(Environment.getExternalStorageDirectory() + "/" + "splah2.jpg")
both are same if SD card is install because,
Environment.getExternalStorageDirectory() returns "/sdcard"