Buongiorno ragazzi, ho un problema nel caricamento di una GridView attraverso la libreria Picasso. L'uso di questa libreria è obbligato per migliorare lo scrolling della GridView. Ma passiamo al problema. In pratica nel momento in cui apro l'activity dove dovrebbero essere visualizzate le immagini con la gridVIew, mi appare una schermata nera. Ecco il codice:
GridViewGalleryActivity la griglia di immagini verrà visualizzata qua:
GridViewGalleryActivitycodice:public class GridViewGalleryActivity extends Activity { private Utils utils; private ArrayList<String> imagePaths = new ArrayList<String>(); private GridViewImageAdapter adapter; private GridView gridView; private int columnWidth; //TODO: 1. CERCARE DI TROVARE SOLUZIONE A PICASSO //TODO: 2. FARE IN MODO CHE QUANDO MODIFICO UN FILE, SI SCELGANO I TAG PREDEFINITI, E LE LOCALITÀ REGISTRATE. //TODO: 3. CERCARE DI FAR RICARICARE AUTOMATICAMENTE LA GRIDVIUE DOPO LA MODIFICA. @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //permette il fullscreen in maniera costante this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_grid_view); gridView = (GridView) findViewById(R.id.grid_view); utils = new Utils(this); // Initilizing Grid View InitilizeGridLayout(); Intent i = getIntent(); imagePaths = i.getStringArrayListExtra("myArray"); // Gridview adapter adapter = new GridViewImageAdapter(GridViewGalleryActivity.this, columnWidth, imagePaths, this); gridView.setOnItemClickListener(OnImageClickListener); gridView.setOnItemLongClickListener(OnImageLongClickListener); // setting grid view adapter gridView.setAdapter(adapter); } AdapterView.OnItemClickListener OnImageClickListener = new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent i = new Intent(GridViewGalleryActivity.this, FullScreenViewActivity.class); i.putExtra("position", position); i.putStringArrayListExtra("myArray", imagePaths); GridViewGalleryActivity.this.startActivity(i); } };
FullScreenViewActivitycodice:public class GridViewImageAdapter extends BaseAdapter { private Activity _activity; private ArrayList<String> _filePaths = new ArrayList<String>(); private int imageWidth; private Context _context; public GridViewImageAdapter(Activity activity, int imageWidth, ArrayList<String> filePaths, Context context) { this._activity = activity; this.imageWidth = imageWidth; this._filePaths = filePaths; this._context = context; } @Override public int getCount() { return this._filePaths.size(); } @Override public Object getItem(int position) { return this._filePaths.get(position); } @Override public long getItemId(int position) { return position; } /* @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; if (convertView == null) { imageView = new ImageView(_activity); } else { imageView = (ImageView) convertView; } // get screen dimensions Bitmap image = decodeFile(_filePaths.get(position), imageWidth,imageWidth); imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); imageView.setLayoutParams(new GridView.LayoutParams(imageWidth, imageWidth)); imageView.setImageBitmap(image); return imageView; }*/ @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView imageView; // check to see if we have a view if (convertView == null) { // no view - so create a new one imageView = new ImageView(_context); } else { // use the recycled view object imageView = (ImageView) convertView; } Picasso.with(_activity) .load(_filePaths.get(position)) .noFade() .centerCrop() .into(imageView); return imageView; } /* * Resizing image size */ public static Bitmap decodeFile(String filePath, int WIDTH, int HIGHT) { try { File f = new File(filePath); BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(new FileInputStream(f), null, o); final int REQUIRED_WIDTH = WIDTH; final int REQUIRED_HIGHT = HIGHT; int scale = 1; while (o.outWidth / scale / 2 >= REQUIRED_WIDTH && o.outHeight / scale / 2 >= REQUIRED_HIGHT) scale *= 2; BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); } catch (FileNotFoundException e) { e.printStackTrace(); } return null; } }
FullScreenImageAdaptercodice:public class FullScreenViewActivity extends Activity { private Utils utils; private FullScreenImageAdapter adapter; private ViewPager viewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_fullscreen_view); viewPager = (ViewPager) findViewById(R.id.pager); Button btnBack = (Button)findViewById(R.id.btn_back); btnBack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { finish(); } }); utils = new Utils(getApplicationContext()); Intent i = getIntent(); int position = i.getIntExtra("position",0); ArrayList<String> imagePath = i.getStringArrayListExtra("myArray"); //TODO CONTROLLARE LA CLASSE FullScreenImageAdapter. CAUSA NULLPOINTIEXCEPTION adapter = new FullScreenImageAdapter(FullScreenViewActivity.this,imagePath); viewPager.setAdapter(adapter); // displaying selected image first viewPager.setCurrentItem(position); } }
COme già ribadito più volte sia nel titolo che all'inizio della discussione, le immagini non vengono caricate e mi si apre una schermata nera. Come posso risolvere? Spero di essere stato chiaro. BUon ferragosto a tutticodice:public class FullScreenImageAdapter extends PagerAdapter { private Activity _activity; private ArrayList<String> _imagePaths; private LayoutInflater inflater; // constructor public FullScreenImageAdapter(Activity activity, ArrayList<String> imagePaths) { this._activity = activity; this._imagePaths = imagePaths; } @Override public int getCount() { return this._imagePaths.size(); } @Override public boolean isViewFromObject(View view, Object object) { return view == ((RelativeLayout) object); } @Override public Object instantiateItem(ViewGroup container, int position) { TouchImageView imgDisplay; Button btnClose; inflater = (LayoutInflater) _activity .getSystemService(Context.LAYOUT_INFLATER_SERVICE); View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container, false); imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay); // BitmapFactory.Options options = new BitmapFactory.Options(); // options.inPreferredConfig = Bitmap.Config.ARGB_8888; // Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position), options); // imgDisplay.setImageBitmap(bitmap); if (position != -1) { Picasso.with(_activity) .load(_imagePaths.get(position)) .noFade() .centerCrop() .into(imgDisplay); } else { Picasso.with(_activity) .load(R.raw.big_problem) .noFade() .centerCrop() .into(imgDisplay); } ((ViewPager) container).addView(viewLayout); return viewLayout; } @Override public void destroyItem(ViewGroup container, int position, Object object) { ((ViewPager) container).removeView((RelativeLayout) object); } }![]()

LinkBack URL
About LinkBacks
Rispondi quotando