0

我的网格视图工作正常并显示来自 SD 卡的图像。当我长按图像时,上下文任务框打开。其中一个选项是查看(全屏查看)。我不知道如何获取 id 或位置选定的图像,以便我可以将其传递给另一个将全屏打开它的活动。

公共类 MainActivity 扩展 Activity {

public class ImageAdapter extends BaseAdapter {

    private Context mContext;
    ArrayList<String> itemList = new ArrayList<String>();

    public ImageAdapter(Context c) {
        mContext = c;   
    }

    void add(String path){
        itemList.add(path); 
    }

    @Override
    public int getCount() {
        return itemList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return itemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(165, 165));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            //imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        Bitmap bm = decodeSampledBitmapFromUri(itemList.get(position), 250, 250);

        imageView.setImageBitmap(bm);
        return imageView;
    }

    public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {

        Bitmap bm = null;
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        bm = BitmapFactory.decodeFile(path, options); 

        return bm;      
    }

    public int calculateInSampleSize(

        BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            if (width > height) {
                inSampleSize = Math.round((float)height / (float)reqHeight);    
            } else {
                inSampleSize = Math.round((float)width / (float)reqWidth);      
            }   
        }

        return inSampleSize;    
    }

}

ImageAdapter myImageAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    myImageAdapter = new ImageAdapter(this);
    gridview.setAdapter(myImageAdapter);

    String ExternalStorageDirectoryPath = Environment
            .getExternalStorageDirectory()
            .getAbsolutePath();

    String targetPath = ExternalStorageDirectoryPath ;

    Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG).show();
    File targetDirector = new File(targetPath);

    File[] files = targetDirector.listFiles();
    for (File file : files){
        myImageAdapter.add(file.getAbsolutePath()); 
    }

    registerForContextMenu(gridview);
}

    @Override  
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
    super.onCreateContextMenu(menu, v, menuInfo);  
        menu.setHeaderTitle("Context Menu");  
        menu.add(0, v.getId(), 0, "View");  
        menu.add(0, v.getId(), 0, "Details");
        menu.add(0, v.getId(), 0, "Delete");

    }  

    @Override  
    public boolean onContextItemSelected(MenuItem item) {  
        if(item.getTitle()=="View"){function1(item.getItemId());}  
        else if(item.getTitle()=="Details"){function2(item.getItemId());}  
        else if(item.getTitle()=="Delete"){function3(item.getItemId());}  
        else {return false;}  
    return true;  
    }  

    public void function1(int id){  
         // Sending image id to FullScreenActivity
        Intent i = new Intent(getApplicationContext(), FullImageActivity.class);

       // Here is the problem i want to get the position or id of the clicked strong textimage and pass it to the other activity so i can view it full screen

        i.putExtra("id", position);
        startActivity(i);
        Toast.makeText(this, "function 1 called", Toast.LENGTH_SHORT).show();  
    }  

}`

4

1 回答 1

0
@Override
    public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

GridView gridview = (GridView) findViewById(R.id.gridview);

myImageAdapter = new ImageAdapter(this);
gridview.setAdapter(myImageAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {  

  @Override  
    public void onItemClick(AdapterView<?> parent, View view, int position,  
    long id) { 
    System.out.println("position"+position);
    }  
 }  
 });  

String ExternalStorageDirectoryPath = Environment
        .getExternalStorageDirectory()
        .getAbsolutePath();

String targetPath = ExternalStorageDirectoryPath ;

Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG).show();
File targetDirector = new File(targetPath);

File[] files = targetDirector.listFiles();
for (File file : files){
    myImageAdapter.add(file.getAbsolutePath()); 
}

registerForContextMenu(gridview);
 }
于 2013-05-07T03:41:18.890 回答