我有来自picasa的图片列表。我把它放在网格视图中然后单击以获取viewpager完成的全屏图像。我尝试了很多方法来获取位图或从view pager适配器获取imageview,但它给出了null。请帮助我,我是新的,这是我的第一个应用程序,我很困惑。 这是我的代码:
package com.example.soha.livingroom;
public class fullimafepager extends Activity implements OnClickListener {
// TouchImageView imgflag;
// BitmapDrawable btmpDr;
private static final String STATE_POSITION = "STATE_POSITION";
// @InjectView(R.id.pager)
LruBitmapCache lreucach;
private PrefManager prefmanger;
private List<String> images = new ArrayList<String>();
private String imageuri;
// Picasa JSON response node keys
// Declare Variables
private ViewPager viewPager;
private ImageAdapter adapter;
private int npostion;
// private Bitmap[] image2;
private int position;
// private im selectedPhoto;
private LinearLayout llSetWallpaper, llDownloadWallpaper;
private Utils utils;
private ProgressBar pbLoader;
private View view;
private Bitmap bitmap;
private ImageView fullImageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get the view from viewpager_main.xml
prefmanger = new PrefManager(getApplicationContext());
setContentView(R.layout.viewpager_main);
if (getIntent().getExtras() != null) {
this.images = getIntent().getExtras().getStringArrayList("list");
position = getIntent().getIntExtra("postion", 0);
;
// fullImageView= images.get(position);
Log.d("=======", "Data " + images.get(1));
Toast.makeText(this, "the" + images.get(1) + position, Toast.LENGTH_LONG).show();
// Intent i = getIntent();
// image selectedPhoto = (image) getIntent().getSerializableExtra(TAG_SEL_IMAGE);
// System.out.println(images);
}
prefmanger = new PrefManager(getApplicationContext());
// Generate sample data
// flag = new int[] { R.drawable.ico_loader, R.drawable.ico_loader, R.drawable.ico_loader, R.drawable.ico_loader};
// Locate the ViewPager in viewpager_main.xml
viewPager = (ViewPager) findViewById(R.id.pager);
// Pass results to ViewPagerAdapter Class
adapter = new ImageAdapter(fullimafepager.this, images);
// Binds the Adapter to the ViewPager
viewPager.setAdapter(adapter);
// viewPager.setCurrentItem(0);
viewPager.setCurrentItem(position);
Log.d("test", "Current Page:"+viewPager.getCurrentItem());
// fullImageView= (ImageView) viewPager.getChildAt(position);
npostion= viewPager.getCurrentItem();
// fullImageView= (TouchImageView) viewPager.findViewById(R.id.imgFullscreen);
fullImageView= (TouchImageView) viewPager.findViewWithTag("pos" + viewPager.getCurrentItem());
Log.d("test", "Current uri:" + fullImageView);
// Log.d("test", "Current Page:"+npostion);
// view = fullimafepager.viewPager.getChildAt(fullimafepager.viewPager.getCurrentItem()).getRootView();
// fullImageView = (ImageView)fullimafepager.viewPager.findViewWithTag("myview" + fullimafepager.viewPager.getCurrentItem());
// Log.d("=======", "Data " + fullImageView);
//addition
// imageUri = images.get(npostion);
// uri = Uri.parse(imageUri);
// imageuri=images.get(npostion);
// bitmap=lreucach.getBitmap(imageuri);
llSetWallpaper = (LinearLayout) findViewById(R.id.llSetWallpaper);
llDownloadWallpaper = (LinearLayout) findViewById(R.id.llDownloadWallpaper);
pbLoader = (ProgressBar) findViewById(R.id.pbLoader);
// hide the action bar in fullscreen mode
// fullImageView = (ImageView) findViewById(R.id.imgFullscreen);
// getActionBar().hide();
// utils = new Utils(getApplicationContext());
// layout click listeners
llSetWallpaper.setOnClickListener(this);
llDownloadWallpaper.setOnClickListener(this);
// setting layout buttons alpha/opacity
llSetWallpaper.getBackground().setAlpha(70);
llDownloadWallpaper.getBackground().setAlpha(70);
}
@Override
public void onClick(View v) {
Bitmap bitmap = ((BitmapDrawable) fullImageView.getDrawable())
.getBitmap();
// Bitmap bitmap = ((BitmapDrawable) imgflag.getBitmap();
/* Bitmap bitmap1= null;
try {
bitmap1 = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(imageuri));
} catch (IOException e) {
e.printStackTrace();
}*/
Log.d("test", "Current uri:" + bitmap);
switch (v.getId()) {
case R.id.llDownloadWallpaper:
// utils.saveImageToSDCard(bitmap);
{ File myDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
prefmanger.getGalleryName());
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Wallpaper-" + n + ".jpg";
File file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
Toast.makeText(
fullimafepager.this,
fullimafepager.this.getString(R.string.toast_saved).replace("#",
"\"" + prefmanger.getGalleryName() + "\""),
Toast.LENGTH_SHORT).show();
// Log.d(this, "Wallpaper saved to: " + file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(fullimafepager.this,
fullimafepager.this.getString(R.string.toast_saved_failed),
Toast.LENGTH_SHORT).show();}}
break;
// button Set As Wallpaper tapped
case R.id.llSetWallpaper:
// utils.shareIt(bitmap);
{ /*Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
// share.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));*/
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "title");
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
OutputStream outstream;
try {
outstream = getContentResolver().openOutputStream(uri);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
outstream.close();
} catch (Exception e) {
System.err.println(e.toString());
}
share.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(share, "Share Image"));
}
break;
default:
break;
}
}
/**
* @author Terry E-mail: yaoxinghuo at 126 dot com
* @version create: 2010-10-21 ??01:40:03
*/
}
这是我的适配器
public class ImageAdapter extends PagerAdapter {
private Activity _activity;
// Declare Variables
Context context;
Utils utiles;
TouchImageView imgflag;
LruBitmapCache lreucach;
private List<String> image2;
private LayoutInflater inflater;
private Bitmap bitmap;
//new
private image selectedPhoto;
public ImageAdapter(Activity activity,
List<String> flag) {
this._activity = activity;
this.image2 = flag;
}
@Override
public int getCount() {
return this.image2.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((TouchImageView) object);
}
@Override
public Object instantiateItem(ViewGroup container, int postion) {
// Declare Variables
inflater = (LayoutInflater) _activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View itemView = inflater.inflate(R.layout.activity_fullscreen_image, container,
false);
imgflag = (TouchImageView) itemView.findViewById(R.id.imgFullscreen);
// fullImageView.setOnTouchListener(this);
// Capture position and set to the ImageView
Picasso.with(_activity).load(image2.get(postion)).into(imgflag);
imgflag.setTag("pos" + postion);
// imgflag.setImageURI(Uri.parse("http:/i.dailymail.co.uk/i/pix/2014/09/27/1411832985119_Puff_Image_galleryImage_SUNDERLAND_ENGLAND_SEPTEM.JPG"));
/* Uri imgUri = Uri.parse(image2.get(postion));
imgflag.setImageURI(null);
imgflag.setImageURI(imgUri);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeFile(image2.get(postion), options);
imgflag.setImageBitmap(bitmap);*/
// bitmap = ((BitmapDrawable) imgflag.getDrawable()).getBitmap();;
// Bitmap bitmap;
// OutputStream output;
// utiles.setBitmap();
// Add viewpager_item.xml to ViewPager
((ViewPager) container).addView(imgflag);
// lreucach.putBitmap(image2.get(postion),bitmap);
Log.d("test", "Adapter creating item:" + postion);
return imgflag;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
// Remove viewpager_item.xml from ViewPager
((ViewPager) container).removeView((TouchImageView) object);
}
@Override
public void restoreState(Parcelable state, ClassLoader loader) {
}
@Override
public Parcelable saveState() {
return null;
}
public Bitmap getBtmap(){
BitmapDrawable btmpDr = (BitmapDrawable) imgflag.getDrawable();
bitmap = btmpDr.getBitmap();
return bitmap;
}