我有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);
}
}
答案 0 :(得分:0)
gridview的OnItemClickListener你必须在调用下一个活动时传递putExtra中的arraylist位置,如下所示:
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);
}
});
发送图片的路径,而不是将位图发送到下一个活动。
答案 1 :(得分: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);
}
});