Android moving a photo from one folder to another - 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.

Related

Storing images in the Android filesystem

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.

How to upload a file and read the contents of a file in android

I am working on an android application where I want users to upload a file and I want to read the contents of uploaded file and display it . I have been reading files from SD card but I now I need the user to upload a file . I searched a lot but I didn't get any solution for it.
My code for reading the file from SD card
File file = new File(dir, "/tounzip/b.txt");
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
DataInputStream input = new DataInputStream(fileInputStream);
Its reading the file contents successfully but Is there any way to make the user upload the file and read the contents from it ? Any help would be great !! Thanks !!
Android file chooser
You require a file picker/file chooser.
Logic is you read the stream and save the stream in your app sandbox (app's memory i.e. /data/package..) then do whatever you want from there.
If file size is small then even a in-memory implementation will help.

Android app generate

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.

Class Recognition; R.drawable.balloons

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?

Unable to take screenshots

I have this code to take screenshots of layouts in Android. It is not throwing any errors, however, the screenshot is not being taken either. Can someone please help me figure out what I am doing wrong here? I am new with Eclipse and I am having a hard time figuring things out. Also if there is any other way to take screenshots can you post it as an answer to this thread? Thanks for your time!
private void getScreenshot()
{
View content = findViewById(R.id.testView);
content.setDrawingCacheEnabled(true);
content.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(content.getDrawingCache());
content.setDrawingCacheEnabled(false);
File file = new File( Environment.getExternalStorageDirectory() + "image.png");
try
{
file.createNewFile();
FileOutputStream ostream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
Do you need to add path separator into your File? i.e.
File file = new File(Environment.getExternalStorageDirectory() +
File.separator + "image.png");
You should add a lot more logs and tests in your code to check whether it is behaving as you would expect, e.g.
Log the details of the file you are trying to create to be sure it is correct.
Once you've created the file, test that it exists, e.g. if (!file.exists())
The Bitmap.compress function returns a boolean, so you should check the return value and log it to see if it succeeded.
One other thought: maybe you need to call ostream.flush() (API docs here) to ensure the buffered data is written to the file?
I'm assuming you're writing this code for use within your app. You probably already know this, but DDMS provides a way to take screenshots in case you just want to take some yourself. Just make sure to select the device to enable the Screenshot menu option.

Categories

Resources