I am having images in SD card and from there i am able to retrieve them and display them in my gallery.
Now i wanted to display the name of that particular image only using Toast.
Can any one tell me how can I do that???My code is as follow
package com.example.Gallery;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.TextView;
import android.widget.Toast;
public class HelloGallery extends Activity {
private TextView Text;
private List<String> ReadSDCard()
{
List<String> tFileList = new ArrayList<String>();
//It have to be matched wi开发者_运维技巧th the directory in SDCard
File f = new File("/sdcard/pictures/");
File[] files=f.listFiles();
if(files != null)//to check whether an image is present in that file or not
{
for(int i=0; i<files.length; i++)
{
File file = files[i];
//to check the type of file and according allowing the files to display
String curFile=file.getPath();
String ext=curFile.substring(curFile.lastIndexOf(".")+1,
curFile.length()).toLowerCase();
if(ext.equals("jpg")||ext.equals("gif")||ext.equals("png"))
/*It's assumed that all file in the path
are in supported type*/
tFileList.add(file.getPath());
}
}
else
{
Text =(TextView)findViewById(R.id.text);
Text.setText("THERE IS NO IMAGE IN THE cARD MOUNTED INTO DEVICE");
}
return tFileList;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageView imageView = (ImageView)findViewById(R.id.image1);
Gallery g = (Gallery) findViewById(R.id.gallery);
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED))
{
final List<String> SD = ReadSDCard();
g.setAdapter(new ImageAdapter(this, SD));
//to saw image with image view
g.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent,
View v, int position, long id) {
//to get a image file
String imageInSD = SD.get(position);
String imagename = imageInSD.intern();
Toast.makeText(HelloGallery.this,
imagename,
Toast.LENGTH_SHORT).show();
//to display image in image view
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
imageView.setImageBitmap(bitmap);
}
});
}
else
{
Text =(TextView)findViewById(R.id.text);
Text.setText("THERE IS NO EXTERNAL CARD MOUNTED IN THE DEVICE");
}
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private List<String> FileList;
public ImageAdapter(Context c, List<String> fList) {
mContext = c;
FileList = fList;
TypedArray a = obtainStyledAttributes(R.styleable.Theme);
mGalleryItemBackground = a.getResourceId(
R.styleable.Theme_android_galleryItemBackground,
0);
a.recycle();
}
public int getCount() {
return FileList.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView,
ViewGroup parent) {
ImageView i = new ImageView(mContext);
Bitmap bm = BitmapFactory.decodeFile(
FileList.get(position).toString());
i.setImageBitmap(bm);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
}
you should use this code to open gallery:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"SelectPicture"),50);
After selecting image from gallery ,control goes to OnActivityResult.
then add this code to OnActivityResult:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 50) {
Uri selectedImageUri = data.getData();
String selectedImagePath = getPath(selectedImageUri);
Toast.makeText(this,selectedImagePath,Toast.LENGTH_LONG).show();
}
}
}
i hope this help you
精彩评论