0

我有 GridView,它正在从 sdcard 获取图像并将其显示在 gridview 中。但是当我单击某些图像时,我想要它。它应该显示在下一个带有其名称的大尺寸活动中。只有当我获取图像时才有可能当我点击它时的名字......我该怎么做......这是我的代码,它只是显示来自 sd 卡的图像

package com.example.imagegallary;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class Album3 extends Activity implements OnClickListener {
      Bitmap bit;
      Button addpicc;
      String    picname ;
      String namee;

    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 arg0) {
   // TODO Auto-generated method stub
   return null;
  }

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

  @Override
  public View getView(final 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(220, 220));
             imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
             imageView.setPadding(8, 8, 8, 8);
             imageView.setOnClickListener(new View.OnClickListener() {

                 @Override
                 public void onClick(View view) {
                   Toast.makeText(getApplicationContext(), ""+position,2).show();
                 }

               });
         } else {
             imageView = (ImageView) convertView;
         }

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

         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.grid);

        GridView gridview = (GridView) findViewById(R.id.myGrid);
        myImageAdapter = new ImageAdapter(this);
        gridview.setAdapter(myImageAdapter);
        addpicc=(Button)findViewById(R.id.addpic);
        addpicc.setOnClickListener(this);



        String ExternalStorageDirectoryPath = Environment
          .getExternalStorageDirectory()
          .getAbsolutePath();
        Intent intent = getIntent();
        //Parcelable myParcelableObject = (Parcelable) i.getParcelableExtra("name_of_extra");

      namee = intent.getStringExtra("name");
        bit = (Bitmap) getIntent().getParcelableExtra("Image");
     picname = intent.getStringExtra("picname");



        Toast.makeText(getApplicationContext(), namee+"gallaryname", 2).show();
        Toast.makeText(getApplicationContext(), picname+"picname", 2).show();

        String targetPath = ExternalStorageDirectoryPath + "/Gallary/"+namee;

      //  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());
         Toast.makeText(getApplicationContext(),file.getAbsolutePath() ,2).show();
        } 
    }

@Override
public void onClick(View v) {
    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    String gallaryname=extStorageDirectory+"/Gallary/"+namee;
     OutputStream outStream = null;

    File file = new File(gallaryname,picname);
    try {
     outStream = new FileOutputStream(file);
     bit.compress(Bitmap.CompressFormat.PNG, 100, outStream);
     outStream.flush();
     outStream.close();

     Toast.makeText(this, "Saved in SD card", Toast.LENGTH_LONG).show();
     Intent i=new Intent(Album3.this,MainApp.class);
     startActivity(i);

    } catch (FileNotFoundException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
     Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}
public void onBackPressed(){
     Intent i=new Intent(Album3.this,addGallaray.class);
     startActivity(i);

    }

}
4

2 回答 2

0

在调用下一个活动时,您必须在 putExtra 中传递 arraylist 位置的 gridview 的 OnItemClickListener,如下所示:

grid.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {
                Intent intent = new Intent(currentactivity.this, nextactivity.class);
                intent.putExtra("path", itemList.get(position));
                startActivity(intent);

            }
        });

发送图像的路径,而不是将位图发送到下一个活动。

于 2013-01-30T05:06:02.723 回答
0

像这样试试

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

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

    gridview.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
            Intent intent = new Intent(Album3 .this, NewActivity.class);
            intent.putExtra("Name", itemList.get(pos));
            startActivity(intent);
        }
    });
于 2013-01-30T05:20:15.540 回答