I can get into my custom directory by this code which displays me the list of images in my directory now what i want is to open those image when i click on a image list .
private void getDir(String dirPath) {
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if (!dirPath.equals(root)) {
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for (int i = 0; i < files.length; i++) {
File file = files[i];
path.add(file.getPath());
if (file.isDirectory())
item.add(file.getName() + "/");
else
item.add(file.getName());
}
ArrayAdapter<String> fileList = new ArrayAdapter<String>(this, R.layout.explorernew, item);
setListAdapter(fileList);
}
File file;
#Override
protected void onListItemClick( ListView l, View v, int position, long id) {
File imgFile = new File(root+"/20150424_173923.jpg");
// TextView stxt = (TextView) findViewById(R.id.textView2);
// stxt.setText(List.get(position));
if (imgFile.exists()) {
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageView);
myImage.setImageBitmap(myBitmap);
}
}
I have hard-code my imgfile as my image name which is displaying the image . I want to dynamically change the image on my selection
the position parameter in the protected void onListItemClick( ListView l, View v, int position, long id) {...} function shows the position of the element in your listView which is selected, so you could get the name of your selected image by String name =(String) l.getItemAtPosition(position); or you can do another thing without using the position parameter and it's to use String name = ((TextView) view).getText();
And then you could easily use the name variable in your line File imgFile = new File(root+name);
Related
I have to fetch all the videos from "/SDCard/Folder/SubFolder" and display them in a GridView.
I am able to save the videos into the desired subfolder created by me and can see them directly from the folder. But I want to show them into a GridView but when I am running my code, It displays nothing but only a black blank screen. Here is my code, please tell me that what mistake I am doing here..
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview_main);
// Check for SD Card
if (!Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
Toast.makeText(this, "Error! No SDCARD Found!", Toast.LENGTH_LONG)
.show();
} else {
// Locate the image folder in your SD Card
file = new File(Environment.getExternalStorageDirectory()
+ File.separator + "/SDCard/Picture/MyCameraVideo");
// Create a new folder if no folder named SDImageTutorial exist
file.mkdirs();
}
if (file.isDirectory()) {
listFile = file.listFiles();
// Create a String array for FilePathStrings
FilePathStrings = new String[listFile.length];
// Create a String array for FileNameStrings
FileNameStrings = new String[listFile.length];
for (int i = 0; i < listFile.length; i++) {
// Get the path of the image file
FilePathStrings[i] = listFile[i].getAbsolutePath();
// Get the name image file
FileNameStrings[i] = listFile[i].getName();
}
}
// Locate the GridView in gridview_main.xml
grid = (GridView) findViewById(R.id.gridview);
// Pass String arrays to LazyAdapter Class
adapter = new GridViewAdapter(this, FilePathStrings, FileNameStrings);
// Set the LazyAdapter to the GridView
grid.setAdapter(adapter);
// Capture gridview item click
grid.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent i = new Intent(ViewActivity.this, ViewImage.class);
i.putExtra("filepath", FilePathStrings);
i.putExtra("filename", FileNameStrings);
i.putExtra("position", position);
startActivity(i);
}
});
}
}
I want to list all the files on the SD card. I use a this code for it:
myList = new ArrayList();
String root_sd = Environment.getExternalStorageDirectory().toString();
file = new File( "storage/" + root_sd ) ;
Log.e(myLog, file.getName());
File list[] = file.listFiles();
for( int i=0; i< list.length; i++)
{
myList.add( list[i].getName() );
Log.e(myLog, list[i].getName() + " : " + list[i].getTotalSpace());
}
And I get a nullPointerException for it after I log the name. I tried an other file also:
file = new File( root_sd ) ;
Ended with the same result.
So, how can I list the files properly? Thx for help!
...//initing the variables
String fileName = Environment.getExternalStorageDirectory().toString();
title.setText(fileName);
ArrayList<String> FilesInFolder = GetFiles(fileName);
mList.setAdapter(new ArrayAdapter<String>(getActivity() , android.R.layout.simple_list_item_1 , FilesInFolder));
mList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Clicking on items
}
});
and the function
public ArrayList<String> GetFiles(String DirectoryPath) {
ArrayList<String> MyFiles = new ArrayList<String>();
File f = new File(DirectoryPath);
f.mkdirs();
File[] files = f.listFiles();
if (files.length == 0)
return null;
else {
for (int i=0; i<files.length; i++)
MyFiles.add(files[i].getName());
}
return MyFiles;
}
If you want a different layout make an own ArrayAdapter
I am using a ListView the size of each cell is very small. I thought to increase size of text so that cell size will adjust automatically. but this didnt work.
My java code is as follows:
View v= findViewById(R.id.rowtext);
myPath = (TextView)findViewById(R.id.path);
root = Environment.getExternalStorageDirectory().getPath();
getDir(root);
}
public void bt_Quit(View v)
{
finish();
}
public void back(View v)
{
getDir(pos);
}
public void home(View v)
{
getDir(root);
}
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
pos=f.getParent();
if(!dirPath.equals(root))
{
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
if(!file.isHidden() && file.canRead()){
path.add(file.getPath());
if(file.isDirectory()){
item.add(file.getName() + "/");
}else{
item.add(file.getName());
}
}
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}
The cellsize of List view is too small. What i want is to know how to adjust cellsize of listView?
change in your xml of listview
android:layout_width="20dp"
android:layout_height="30dp"
Change numbers according to ur need
this is my file browser code below when show files from sd card i put multiple apk files in sd card but this code just show apk files in sd card not install when click what do ido? please help me. i want to run apk files which show on listview but is not run how do i modify this code to run selected file??
public class MainActivity extends ListActivity {
private List<String> item = null;
private List<String> path = null;
private String root;
private TextView myPath;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myPath = (TextView)findViewById(R.id.path);
root = Environment.getExternalStorageDirectory().getPath();
getDir(root);
}
private void getDir(String dirPath)
{
myPath.setText("Location: " + dirPath);
item = new ArrayList<String>();
path = new ArrayList<String>();
File f = new File(dirPath);
File[] files = f.listFiles();
if(!dirPath.equals(root))
{
item.add(root);
path.add(root);
item.add("../");
path.add(f.getParent());
}
for(int i=0; i < files.length; i++)
{
File file = files[i];
if(!file.isHidden() && file.canRead()){
path.add(file.getPath());
if(file.isDirectory()){
item.add(file.getName() + "/");
}else{
item.add(file.getName());
}
}
}
ArrayAdapter<String> fileList =
new ArrayAdapter<String>(this, R.layout.row, item);
setListAdapter(fileList);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
File file = new File(path.get(position));
if (file.isDirectory())
{
if(file.canRead()){
getDir(path.get(position));
}else{
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "] folder can't be read!")
.setPositiveButton("OK", null).show();
}
}else {
new AlertDialog.Builder(this)
.setIcon(R.drawable.ic_launcher)
.setTitle("[" + file.getName() + "]")
.setPositiveButton("OK", null).show();
}
}
}
Implement a ListView (follow this TUTORIAL).
Then implement a listener to handle the click on the items ( listView.onItemClickListener(); )
and then use the snipper below for the installation:
Intent promptInstall = new Intent(Intent.ACTION_VIEW)
.setData(Uri.parse("file:///path/to/your.apk"))
.setType("application/vnd.android.package-archive");
startActivity(promptInstall);
ref: https://stackoverflow.com/a/4604922/847818
You have to go step-by-step.
How to show the name of the picture in the gallery by clicking on it?
i made an array of my images and I get the / sdcard / CameraExample / 28.06.2011_11-23-54.bmp, but it displays only 1 of 17. I would like to know how to remove a directory file, leaving only its name. and how do I assign getname each picture in the array?
it's my sample code.
Gallery g = (Gallery) findViewById(R.id.Gallery01);
g.setAdapter(new ImageAdapter(this, ReadSDCard()));
final TextView label2 = (TextView)findViewById(R.id.TextView01);
final TextView label = (TextView)findViewById(R.id.textView1);
label.setText("Фото 0 из" + g.getAdapter().getCount());
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View v, int position, long id) {
label.setText("Фото"+" "+ ++position + " из " + parent.getCount());
String dirPath = "/sdcard/CameraExample/";
File f = new File(dirPath);
File[] files = f.listFiles();
for(int i=0; i < files.length; i++)
{
File file = files[i];
String fileName = file.getName();
if (i == position)
{
label2.setText(fileName);
}
}
}
});
Thanks
Dude you should be setting the name of the file in the label instead of setting its postion.
Check your code over here:
label.setText("Photo" + ++position + " of " + parent.getCount() + file);
Modify this to as:-
String fileName = file.getName();
label.setText("Viewing file: " + fileName);
This should work for you.