2011-09-22 93 views
0

我有SD卡中的图像,从那里我能够检索它们并将它们显示在我的画廊中。 要将图像名称存储在SD卡的Android模拟器

现在我想仅使用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; 
    } 
} 

}

+0

你可以张贴一些代码或细节你是如何处理的解决方案?这些通用问题并不是真正的问题。 – Phil

+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(); 
} 
} 
} 

我希望这会帮助你

+0

阿比感谢你的答复,但你可以帮我我的代码,我刚才发布了...我想获取图像的名称,我选择并希望将其显示到我的屏幕上..thnks再次1nce –

+0

你尝试我的代码,selectedImagePath是图像路径名称,图像名称 – Abhi

+0

已经获得filepath到arraylist,因此尝试只提取图像名称到另一个数组列表,并根据图库中图像的位置显示它 – Abhi