2011-09-22 94 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