An Android novice here.
I'm trying to complete a task which involves creating a simple app containing buttons on a single page. Each button, when clicked, should display the corresponding image.
One thing I don't understand in the instructions is that "the images should be stored on the phone filesystem rather than compiled into the application under
resources". What exactly does this mean? Do I need to load the images into the phone manually every time I try running the application? Any guidance would be appreciated.
private void saveImage(Bitmap finalBitmap, int i ) {
File file = new File (path+name.jpg);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 50, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
This method will save bitmap as a jpeg file on your phone.
P.S.
path - path of place where you want to save
name - name of image
Apps can include images in their resources/drawable folder that get added into the actual app .apk file. That makes them retrievable using R.drawable.image_name. Sounds like the instructions you are following does not want you do this. They want you to store them on the phone in the data/data/package file structure. If this is the case you can find plenty of examples on how to do this. The answer by Arsen Sench here does this.
Related
I want to generate a qr code image and add it programmatically to the assets drawable folder of the app.
In the mean time, you would add it mannually in eclipse or android studio. Just wonder is there any ways to do it programmatically as well.
Many thanks!
This is simply not possible, you cant't modify/add that folder once you have generated apk and installed app. What you can do is to generate a folder on internal or external storage and save your images there.
It is already disccussed here and here
Asset Folder is used to load our Data with application , it never be changed at run time , AssetManger has method to read Asset Data and there is no way to write within Asset programmatically at Run Time.
Rather if you want to store your data at run time , You may store in Internal Memory like below Code.
Drawable drawable = getResources().getDrawable(R.drawable.demo_img);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
bitmap.compress(Bitmap.CompressFormat.PNG, 60, bytearrayoutputstream);
file = new File( Environment.getExternalStorageDirectory() + "/SampleImage.png");
try
{
file.createNewFile();
fileoutputstream = new FileOutputStream(file);
fileoutputstream.write(bytearrayoutputstream.toByteArray());
fileoutputstream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
I am making a quiz app which requires me to be able to get an image from a database BLOB or image path stored in the database. However i have looked around and a lot of people suggest using a file path, the problem is i don't know where to store the image if i use the file path method.
Do i store it somewhere in the app such as the resources folder?, a lot of examples use SD cards but is it possible to save an image to SD card from a database and if so surely that would mean i have two images one in database and one on SD card.
Where is the best place to store a images for a quiz app that i can use on any phone an will have access to said images? and how ?.
Thanks in advance.
Image storage must be performed in some directory and the corresponding paths of the image must be stored in the database.
There will be times when you will accessing your images from one acivity then the other, in that case you will just need to pass the path of the image from activity one to activity two and then retrieve the image from the directory to display in activity two.
Image storing and loading from databases may turn out to be a pain when the size of the images will start increasing.
For learning how to store images, Give an eye to this
CODE EXAMPLE
private void SaveImage(Bitmap finalBitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
In the above code, the line
Environment.getExternalStorageDirectory()
is refering to android/data folder. you can create folder inside upto any level, like android/data/folderone/folderTwo/folderThree
.
Note: However you need to first fetch the images from server for the first time and store them in device.If you are thinking of bundling up the images along with the app, put all of your images in res/drawable folders.(if no web server functionality is involved)
I'm having a little problem with my android app.
My app generates a .html file when a "export button" is pressed.
But I can't see the file in my pc or in the Android's Download app. I can only see it in Astro file manager.
That's how I generate and saved my file .
String string = "Hello World"
String filename = "/sdcard/Download/teste.html";
FileOutputStream outputStream;
try {
File file = new File(filename);
boolean newFile = file.createNewFile();
if(!newFile){ //if the file exists I delete it and generate a new file
file.delete();
newFile=file.createNewFile();
}
Context context=getActivity();
FileOutputStream fOut = new FileOutputStream(file,true);
// Write the string to the file
fOut.write(string.getBytes());
/* ensure that everything is
* really written out and close */
fOut.flush();
fOut.close();
}
catch (Exception e) {
e.printStackTrace();
}
I suppose there is a way to visualize this file without the Astro app but I can't find how do this, if someone can help I'll be grateful.
Thanks
First, never hardcode paths. Your path will be wrong on some Android devices. Please use the proper methods on Environment (e.g., getExternalStoragePublicDirectory()) or Context (e.g., getExternalFilesDir()) to get the roots under which you can safely place files.
Beyond that, files that you write to external storage will not be visible to PCs until that file is indexed by MediaScannerConnection, and even then it might require the user to perform some sort of "reload" or "refresh" operation in their file browser to see it.
I have another blog post with more about external storage which may be of use to you.
Okay, I seem to be having a small issue with R.drawable.balloons. I'm trying to use a template for building a private external storage file that I found on Android Developer, but balloons keeps giving an error (cannot be resolved or is not a field). I was wondering if I could get some help fixing it.
Here's the code section it sits in:
void createExternalStoragePrivateFile() {
// Create a path where we will place our private file on external
// storage.
File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
try {
/*
Very simple code to copy a picture from the application's
resource into the external file. Note that this code does
no error checking, and assumes the picture is small (does not
try to copy it in chunks). Note that if external storage is
not currently mounted this will silently fail.
*/
// Creates file to stream picture
OutputStream os = new FileOutputStream(file);
// Allows app to accept the picture
InputStream is = getResources().openRawResource(R.drawable.balloons);
byte[] data = new byte[is.available()];
is.read(data);
os.write(data);
is.close();
os.close();
} catch (IOException e) {
// Unable to create file, likely because external storage is
// not currently mounted.
Log.w("ExternalStorage", "Error writing " + file, e);
}
}
A heads up, in case I get called out for being a copy/paster, this is only supposed to be a template, but I would like to test that it works before I make changes. Sorry.
You need an image named balloons in the res\drawable folder of your project (or any of its variants, such as drawable-hdpi, &c). The R class is autogenerated.
See How do I add R drawable android?
I'm using the below code to transfer an image from one folder on external memory, to another..as specified by the user. The problem is, the photo gets copied to the destination folder fine..but I can't open or view it. I use a file manager named Astro to see if it was successfully moved, and it is..but I'm unable to open it both in Astro and in the resident Gallery app. I'm thinking something is wrong with my code and maybe I need read and/or decode to photo before I can move it, from what I understand about the File class it is just an abstraction. Any guidance on this would be greatly appreciated, here is the code I'm currently using.
File img = new File(imgViewPath);
File output = new
File(Environment.getExternalStorageDirectory().toString() + "/MyAppPics/" + moved,
img.getName());
OutputStream out = null;
try {
out = new BufferedOutputStream(new FileOutputStream(output));
}
finally {
if (out != null) {
out.close();
}
}
}catch(Exception e){
e.printStackTrace();
}
You are not writing anything to the output stream. Read the bytes from the input stream and write it to the output stream, only then the files will get copied.