将图像名称存储在android模拟器的SD卡中

时间:2011-09-22 12:52:54

标签: android android-layout android-widget

我在SD卡中有图像,从那里我可以检索它们并在我的画廊中显示它们。

现在我只想使用Toast显示该特定图像的名称。

谁能告诉我怎么办呢?

我的代码如下

 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 with 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;
     }
 }

}

1 个答案:

答案 0 :(得分:0)

您应该使用此代码打开图库:

Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,"SelectPicture"),50);

从图库中选择图像后,控件转到OnActivityResult。

然后将此代码添加到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();
}
}
}

我希望这可以帮到你