In my application I save the contents of a tableLayout as an image in a folder. To allow user to open a file from the saved images I've created a text file that contains the names of these files. These file names will be loaded in an array (files) later. User clicks on "open" to see a list of file names and selects the one he wants to open. I'm using the following code to open a file.
final String imageInSD = extStorageDirectory+"/myFolder/"+files[which];
//where 'files' is an array of strings and contains the names of files.
//and 'which' is the index of the selected element in the list
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
ImageView ivv=(ImageView) findViewById(R.id.imageView);
ivv.setImageBitmap(bitmap);
when I try it nothing happens so I tried the following
final String imageInSD = extStorageDirectory+"/myFolder/myFile.PNG";
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
ImageView ivv=(ImageView) findViewById(R.id.imageView);
ivv.setImageBitmap(bitmap);
And it shows the image named myFile.
I've already checked if I'm getting the correct file name and path and it seems to be correct. (when i click on myFile.PNG in the list and show the path I get "/mnt/sdcard/myFolder/myFile.PNG").
Why doesn't it work when I use the first code?
String concatenation isn't a good way to combine paths. It is better to use the File constructor :
File directory = new File(extStorageDirectory, "myFolder");
File fileInDirectory = new File(directory, files[which]);
Bitmap bitmap = BitmapFactory.decodeFile(fileInDirectory.getAbsolutePath());
Related
My requirement is to open all images and video's of specific folder.
I have refereed this link, Now I am able to show a image in gallery but I want to show all images from a specific folder. Almost all link I have tried on stack but did not get success.
You can set path in File object initialize at that time.
File folder = new File("/sdcard/Photo/"); in this tutorial default path is /sdcard/photo/ at this place you can set your path then get your files.
I sinc it is not good idea but you may write your own searcher in all files on mobile phone for example
public ArrayList<File> getAllphotos(String path){
ArrayList<File> photoPath = new ArrayList<>();
File yourDir = new File(path);
for (File f : yourDir.listFiles()) {
String mas[] = f.toString().split("\\.");
if(mas[mas.length - 1].equalsIgnoreCase("png") || mas[mas.length - 1].equalsIgnoreCase("jpeg")){//or other formats
//it is picture
photoPath.add(f);
}
}
return photoPath;
}
call this wis iternal and external storage
I am trying to use PDFjet library to print a page to PDF in Android.
The documentation tells me that in order to print an image to PDF, I need to get a handle on the image filename like:
String fileName = "images/myimage.png";
So now i want to print an image from the res/drawable folder. In Android the resources are referenced through R.id which returns an int.
My question is:
How do i get a handle on the image from the drawable folder as string ?
try this:
String fileName = "android.resource://your.package.name/" + R.drawable.xxx;;
When I save the image to the sdcard it automatically saves it with the given file name. If a file with the same name already exist, it is replaced.
I want to store the images with file names in a sequence where if the file Image exists it will be saved with image1 next image2 and so on.
Please advise how to do that, as I am new to programming.
File fileDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File[] images = fileDir.listFiles(new FilenameFilter(){
public boolean accept(File file, String name){
if(name,toLowerCase().startsWith("image"));
return true;
else return false;
}});
File fileToSave;
if(images.length > 0)
fileToSave = new File(fileDir.getAbsolutePath(), "image" + images.length);
else
fileToSave = new File(fileDir.getAbsolutePath(), "image");
something like that'll do.
you must do two things
1. check is there file with same name
2. change name of the file you want to save
It's simple I think , you can also try to open this file named e.g. "name" , if it opens, change the "name" to "name1" and try it again.
you must keep variable named string e.g. String newName , than
String newName = "name" + 1 , something like this
Regards Hayk Nahapetyan
Cut and paste the image you want to name from the SD card to the internal storage. After renaming, copy the images to SD card or use it as a backup.
I'm attempting to create a gallery/gridview that is loaded with images from a specific folder that resides on an SDCard. The path to the folder is known, ("mnt/sdcard/iWallet/Images") , but in the examples I've seen online I am unsure how or where to specify the path to the pictures folder I want to load images from. I have read through dozens of tutorials, even the HelloGridView tutorial at developer.android.com but those tutorials do not teach me what i am seeking.
Every tutorial I have read so far has either:
A) called the images as a Drawable from the /res folder and put them into an array to be loaded, not using the SDCard at all.
B) Accessed all pictures on the SDCard using the MediaStore but not specifying how to set the path to the folder I want to display images form
or
C) Suggested using BitmapFactory, which I haven't the slightest clue how to use.
If I'm going about this in the wrong way, please let me know and direct me toward the proper method to do what I'm trying to do.
my target android sdk version 1.6...
thanks..
You can directly create Bitmaps from decodeFile (String pathName) that will give you Bitmap object that can be set on ImageView
Update: Below is sudo code with minor errors modify it to suit your needs
File path = new File(Environment.getExternalStorageDirectory(),"iWallet/Images");
if(path.exists())
{
String[] fileNames = path.list();
}
for(int i = 0; i < fileNames .length; i++)
{
Bitmap mBitmap = BitmapFactory.decodeFile(path.getPath()+"/"+ fileNames[i]);
///Now set this bitmap on imageview
}
Actually, you are wrong to mention fixed path to access SD-card directory, because in some device it is /mnt/sdcard and in other /sdcard.
so to access root directory of sd-card, use the getExternalStorageDirectory(), it gives you actual path of root directory.
This function will resturn all the files from specific folder you need to pass path till ur folder
public static List getFilesFromDir(File aStartingDir)
{
List result = new ArrayList();
File[] filesAndDirs = aStartingDir.listFiles();
List filesDirs = Arrays.asList(filesAndDirs);
Iterator filesIter = filesDirs.iterator();
File file = null;
while ( filesIter.hasNext() ) {
file = (File)filesIter.next();
result.add(file); //always add, even if directory
if (!file.isFile()) {
//must be a directory
//recursive call!
List deeperList = getFileListing(file);
result.addAll(deeperList);
}
}
Collections.sort(result);
return result;
}
BitmapDrawable d = new BitmapDrawable(getResources(), path+".jpg"); // path is ur resultant //image
img.setImageDrawable(d);
Hope it help u...
You can access your directory using File java class, then iterate through all the files in there, create a bitmap for each file using Bitmapfactory.decodeFile() then add the bitmaps to your gallery.
I've the file name of an image stored in the drawable? how can I open it? I want to know the path?
the file name is String imageFileName = "image.png"
and I want to have the path string something like file://drawable/image.png
You can open it using openRawResource(). See more at the documentation.
See this post regarding how to construct a uri to raw resource, though as far as I know it is not recommended.
You can access it through its resource ID. See previous answers such as this one:
Select Drawable file path
Here is the code segment showing how you can refer to an image through its resource ID.
Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.pic1);
int pic_width = mBitmap.width();
int pic_height = mBitmap.height();
See here for more detail