Can anyone explain why downloading/playing a video from my applications cache directory does not work, but downloading/playing the same video from my sdcard does work?
Note: this video is being downloaded. I am saving to memory before calling VideoView.setVideoPath(...).
// Works
File file = new File(Environment.getExternalStorageDirectory(), "vid-test.3gp");
// Does not work
File file = new File(getCacheDir(), "vid-test.3gp");
In each case the file in question does exist.
If I attempt to call VideoView.setVideoURI(...) and "stream" the video to my VideoView, it is hit and miss whether or not it will work.
Can anyone explain this behavior?
It's probably a permission issue. Here is a working snipped:
InputStream in = connection.getInputStream();
File file = new File(getApplicationContext().getCacheDir() ,fileName);
if(!file.exists()){
file.setReadable(true);
file.createNewFile();
if (file.canWrite()){
FileOutputStream out = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int len1 = 0;
while ( (len1 = in.read(buffer)) > 0 ) {
out.write(buffer,0, len1);
}
out.close();
}
in.close();
Runtime.getRuntime().exec("chmod 755 "+getCacheDir() +"/"+ fileName);
}
Related
I'm currently developing an app that need to check if there is a new version available.
I found how to download the apk and install it.
But i cant figure out how to get the APK URL.
I'm using the testing (alpha) environment, so my app is not published yet, where can i get the APK URL.
Here's the code for download:
try {
URL url = new URL(apkurl);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = Environment.getExternalStorageDirectory() + "/download/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "app.apk");
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();//till here, it works fine - .apk is download to my sdcard in download file
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + "app.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Update error!", Toast.LENGTH_LONG).show();
}
How can i get the APK URL?
you have to create it yourself. create folder and name a file to it. as you are doing in the code. like
String PATH = Environment.getExternalStorageDirectory() + "/download/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "app.apk");
to create your own one do this.
Add this permission in Manifest,
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
and to create folder with name of your file
File folder = new File(context.getCacheDir().getPath() + "/files/myFolder");
folder.mkdirs();
File f= new File(folder, "app.apk");
if (!folder .exists()){
folder.create();
}
where you can put your apk
If you are using Alpha (testing) environment. This is the Url:
https://play.google.com/apps/testing/com.yourdomain.package
But, your APK will not be available to public, it is ONLY for activated testers in the alpha section.
I'm trying to create a folder on external storage with no success.Although i've managed to create a folder in my app's directory, i can't do the same for external storage and i also get false when i call canWrite().I have declared the WRITE_EXTERNAL_PERMISSION on manifest.
Here is my code for my app's directory
File file1=new File(context.getFilesDir(), "//test1");
file1.mkdir();
System.out.println(file1.canWrite());
and for ExternalStorage respectively
File file2=new File(Environment.getExternalStorageDirectory(), "//test2");
file2.mkdir();
System.out.println(file2.canWrite());
In the first case the folder gets created and i get true on println.On the second one folder does not get created and i get false on println.
Add Below Permission i your manifest.xml
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
String PATH = Environment.getExternalStorageDirectory() + "/download/YourPath";
String fileName="Name for your File";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, fileName);
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1)
{
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
This will create a directory,if you use comma,it means "//test2" will become a file name
String string = Environment.getExternalStorageDirectory().getPath() + "/test2";
File file = new File(string, "file_name");
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(your_data);
fileOutputStream.flush();
fileOutputStream.close();
I'm writing a process that downloads/ copies a file attached to Gmail on to the SD card that my application can then read.
When a user clicks on the attachment my activity is fired and I use the following code to save to local storage;
InputStream in = getContentResolver().openInputStream( intent.getData() );
String ext = intent.getType().equals("text/xml") ? ".xml" : ".gpkg";
localFile = new File( TILE_DIRECTORY, "tmp/"+intent.getDataString().hashCode()+ext);
// If we haven't already cached the file, go get it..
if (!localFile.exists()) {
localFile.getParentFile().mkdirs();
FileIO.streamCopy(in, new BufferedOutputStream(new FileOutputStream(localFile)) );
}
The FileIO.streamCopy is simply;
public static void streamCopy(InputStream in, OutputStream out) throws IOException{
byte[] b = new byte[BUFFER];
int read;
while ((read = in.read(b)) != -1) {
out.write(b, 0, read);
}
out.close();
in.close();
}
This all works fine on a small file, but with a 6Mb attachment only 12Kb is ever getting written. I'm not even getting an error, the process just runs through very quickly and i'm left with a corrupt file.
This process is run in its own thread and is part of a larger app with a lot of fileIO, so there is no issue with permissions/ directories etc.
Is there something different I should be doing with the stream?
Incidentally, intent.getData() is
content://gmail-ls/mexxx#gmail.com/messages/6847/attachments/0.1/BEST/false
and intent.getType() is
application/octet-stream
Thanks
All work's fine with this code
InputStream in = new BufferedInputStream(
getContentResolver().openInputStream(intent.getData()) );
File dir = getExternalCacheDir();
File file = new File(dir, Utils.md5(uri.getPath()));
OutputStream out = new BufferedOutputStream( new FileOutputStream(file) );
streamCopy(in, out);
I have a png file in the raw folder. I get the inputStream using :
inputStream = getResources().openRawResource(R.raw.test);
I am trying to write this inputStream in a new file in an Android application. This is my code:
inputStream = getResources().openRawResource(R.raw.test);
File file = new File("/test.png");
outputStream = new FileOutputStream(file);
int read = 0;
byte[] bytes = new byte[1024*1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.close();
inputStream.close();
When I run the application, I get the following error in the logcat:
java.io.FileNotFoundException: /test.png: open failed: EROFS (Read-only file system)
Basically I want to create a File object so that I can send this to my server.
Thank you.
You will not have access to the file-system root, which is what you're attempting to access. For your purposes, you can write to internal files new File("test.png"), which places the file in the application-internal storage -- better yet, access it explicitly using getFilesDir().
For truly temporary files, you might want to look into getCacheDir() -- should you forget to delete those temporary files, the system will reclaim the space when it runs out of room.
Here's my solution:
inputStream = getResources().openRawResource(R.raw.earth);
file = new File(Environment.getExternalStorageDirectory() + File.separator + "test.png");
file.createNewFile();
outputStream = new FileOutputStream(file);
int read = 0;
byte[] bytes = new byte[1024*1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.close();
inputStream.close();
I was using this code to download a pdf file from server but it is taking a hell lot of time in that the file is around 3 GB .Is there any other way to solve this problem? please help
FileOutputStream f = new FileOutputStream(file);
byte[] buffer = new byte[1024*10000];
int len1 = 0;
while ( (len1 = in.read(buffer)) > 0 ) {
f.write(buffer,0, len1);
}
f.close();
OutputStream outStream = null;
outStream = new FileOutputStream(file);
outStream.close();
You can use DownloadManager apis to download any files. Android will download the file for you