Hi i am a noob to android, I am trying to load some data to a ListView using custom adapter. I am using the following code for loading data. First time, it working well. But while I am trying to load more, then data loads and image is showing random while scrolling. After showing some random images in that list finally shows the correct image. It is repeating on the next scroll also
Here is my getView code
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
final ViewHolder holder;
if (v == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.review_list_m, null);
v.setMinimumHeight(height);
holder = new ViewHolder();
holder.item1 = (TextView) v.findViewById(R.id.name);
holder.image = (ImageView) v.findViewById(R.id.posterView);
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
int colorPos = position % colors.length;
v.setBackgroundColor(Color.parseColor(colors[colorPos]));
final Custom custom = entries.get(position);
if (custom != null) {
holder.image.setBackgroundColor(Color.parseColor(colors[colorPos]));
holder.image.setLayoutParams(new LinearLayout.LayoutParams(height-10,width-10));
String imgUrl=custom.getImage();
AsyncHttpClient client = new AsyncHttpClient();
String[] allowedContentTypes = new String[] { "image/png", "image/jpeg" };
client.get(imgUrl, new BinaryHttpResponseHandler(allowedContentTypes) {
#Override
public void onSuccess(byte[] fileData) {
// Do something with the file
ByteArrayInputStream inputStream = new ByteArrayInputStream(fileData);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
holder.image.setImageBitmap(bitmap);
}
});
holder.item1.setHeight(height/3);
Log.v("PATH",custom.getcustomBig());
holder.item1.setText(custom.getcustomBig());
}
return v;
}
Any idea ? Please help
UPDATE
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
final ViewHolder holder;
if (v == null) {
LayoutInflater vi =
(LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.review_list_m, null);
v.setMinimumHeight(height);
holder = new ViewHolder();
holder.item1 = (TextView) v.findViewById(R.id.name);
holder.image = (ImageView) v.findViewById(R.id.posterView);
v.setTag(holder);
}
else
holder=(ViewHolder)v.getTag();
int colorPos = position % colors.length;
v.setBackgroundColor(Color.parseColor(colors[colorPos]));
final Custom custom = entries.get(position);
if (custom != null) {
holder.image.setBackgroundColor(Color.parseColor(colors[colorPos]));
holder.image.setLayoutParams(new LinearLayout.LayoutParams(height-10,width-10));
final String imgUrl=custom.getImage();
holder.image.setTag(imgUrl);
holder.image.setImageBitmap(null);
Bitmap cachedBitmap = cache.get(imgUrl);
if( cachedBitmap == null) {
Log.v("HERE","DOWNLOADING");
AsyncHttpClient client = new AsyncHttpClient();
String[] allowedContentTypes = new String[] { "image/png", "image/jpeg" };
client.get(imgUrl, new BinaryHttpResponseHandler(allowedContentTypes) {
#Override
public void onSuccess(byte[] fileData) {
// Do something with the file
ByteArrayInputStream inputStream = new ByteArrayInputStream(fileData);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
holder.image.setImageBitmap(bitmap);
}
});
}
else
{
holder.image.setImageBitmap(cachedBitmap);
}
holder.item1.setHeight(height/3);
//Log.v("PATH",custom.getcustomBig());
holder.item1.setText(custom.getcustomBig());
}
Problably you should set null bitmap to holder.image if v != null. Otherwise Android can show bitmap from other cell until new image is downloaded via async http request.
Example code (for problem with redownload images), it wasn't testes, but should give you idea how should it looks:
HashMap<String, Bitmap> cache = new HashMap<String, Bitmap>();
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//....
final Custom custom = entries.get(position);
if (custom != null) {
holder.image.setBackgroundColor(Color.parseColor(colors[colorPos]));
holder.image.setLayoutParams(new LinearLayout.LayoutParams(height-10,width-10));
String imgUrl=custom.getImage();
Bitmap cachedBitmap = cache.get(imgUrl);
if( cachedBitmap == null) {
AsyncHttpClient client = new AsyncHttpClient();
String[] allowedContentTypes = new String[] { "image/png", "image/jpeg" };
client.get(imgUrl, new BinaryHttpResponseHandler(allowedContentTypes) {
#Override
public void onSuccess(byte[] fileData) {
// Do something with the file
ByteArrayInputStream inputStream = new ByteArrayInputStream(fileData);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
holder.image.setImageBitmap(bitmap);
cache.add( imgUrl, bitmap );
}
});
}
else
{
holder.image.setImageBitmap(cachedBitmap);
}
holder.item1.setHeight(height/3);
Log.v("PATH",custom.getcustomBig());
holder.item1.setText(custom.getcustomBig());
}
EDITed answer
Validate your imageview's tag hasn't change before you set the Bitmap.
You could try:
/**
* Caches an image (or a group of them) async.
* #author
*
*/
public static class ImageCacher extends AsyncTask<String, String, Integer>{
Context context;
ImageView iv;
Bitmap b;
public ImageCacher(Context context, ImageView iv){
this.context = context;
this.iv = iv;
}
#Override
protected Integer doInBackground(String... params) {
for(final String param:params){
//check if already CACHED
String filename = String.format("%d", param.hashCode());
File file = new File(context.getFilesDir(), filename);
if(file.exists()){
try {
b = BitmapFactory.decodeFile(file.getAbsolutePath());
} catch (Exception e) {
}
if(b != null){
if(iv != null){
iv.post(new Runnable() {
public void run() {
String tag = (String) iv.getTag();
if(tag != null){
if(tag.matches(param))
iv.setImageBitmap(b);
}
}
});
}
return 0;
}else{
file.delete();
}
}
//download
file = new File(context.getFilesDir(), filename);
b = saveImageFromUrl(context, param);
if(b != null){
if(iv != null){
iv.post(new Runnable() {
public void run() {
String tag = (String) iv.getTag();
if(tag != null){
if(tag.matches(param))
iv.setImageBitmap(b);
}
}
});
}
}
}
return 0;
}
}
/**
* Gets an image given its url
* #param param
*/
public static Bitmap saveImageFromUrl(Context context, String fullUrl) {
Bitmap b = null;
try {
URL url = new URL(fullUrl);
URLConnection conn = url.openConnection();
conn.setDoInput(true);
conn.connect();
//save bitmap to file
InputStream is = conn.getInputStream();
String filename = String.format("%d", fullUrl.hashCode());
File file = new File(context.getFilesDir(), filename);
if(file.exists()){
//delete
file.delete();
file=null;
}
file = new File(context.getFilesDir(), filename);
FileOutputStream out = new FileOutputStream(file);
byte buffer[] = new byte[256];
while(true){
int cnt = is.read(buffer);
if(cnt <=0){
break;
}
out.write(buffer,0, cnt);
}
out.flush();
out.close();
is.close();
b = BitmapFactory.decodeFile(file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
return b;
}
/**
* Gets an already cached photo
*
* #param context
* #param fullUrl
* #return
*/
public static Bitmap getCachedPhoto(Context context, String fullUrl){
System.gc();
String filename = String.format("%d", fullUrl.hashCode());
File file = new File(context.getFilesDir(), filename);
if(file.exists()){
try {
Bitmap b = BitmapFactory.decodeFile(file.getAbsolutePath());
return b;
} catch (Exception e) {
}
}
return null;
}
And then, to set the image in the adapter, you can do like:
//set info
vh.iv.setTag("");
String foto = your_url_goes_here;
vh.iv.setImageResource(R.drawable.fotodefault_2x);
if(!TextUtils.isEmpty(foto)){
vh.iv.setTag(foto);
Bitmap b = getCachedPhoto(context, foto);
if(b != null){
vh.iv.setImageBitmap(b);
}else{
new ImageCacher(context, vh.iv).execute(foto);
}
}
Don't forget to correctly encapsulate the class and methods.
Hope it helps.
I had this random pictures problem with my arrayAdapter. Then I used Picasso which takes care of imageView reusing in listview: http://square.github.io/picasso/
Example of code:
Picasso.with(mContext)
.load(url)
.resize(size, size)
.centerCrop()
.placeholder(R.drawable.image_holder)
.into(holder.imgDefaultImage);
Related
I have 2 apps that I have PDFs in the asset folders. In app A the code works perfectly fine but in app B it doesn't work. I don't get any errors. When clicking the button in app B it opens adobe acrobat but I get a toast saying "This file could not be accessed. check the location or network and try again". I even put the PDF in app A to make sure nothing was wrong with this PDF file. It opened fine in app A.
Here is my code for app B. I'm trying to open it via a button in a list.
[CODE]
public class InteractiveArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
static final String TAG = InteractiveArrayAdapter.class.getSimpleName();
String ta = "helen.pdf";
GPXParser mParser = new GPXParser();
public InteractiveArrayAdapter(Context context, String[] values) {
super(context, R.layout.rowbuttonlayout, values);
this.context = context;
this.values = values;
}
static class ViewHolder {
protected TextView text,header, number, web;
protected ImageButton web_pic, imageView;
ImageView bike, atv, utv;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (convertView == null) {
LayoutInflater inflator = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflator.inflate(R.layout.rowbuttonlayout, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.header = (TextView) view.findViewById(R.id.header);
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.bike = (ImageView) view.findViewById(R.id.bike);
viewHolder.atv = (ImageView) view.findViewById(R.id.atv);
viewHolder.utv = (ImageView) view.findViewById(R.id.utv);
viewHolder.web_pic = (ImageButton) view.findViewById(R.id.web_pic);
viewHolder.imageView = (ImageButton) view.findViewById(R.id.check);
view.setTag(viewHolder);
viewHolder.imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(context, MapActivity.class);
context.startActivity(myIntent);
}
});
viewHolder.web_pic.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
copyReadAssets();
}
});
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.header.setText(values[position]);
holder.imageView.setImageResource(R.drawable.map1);
holder.web_pic.setImageResource(R.drawable.pdf);
holder.bike.setImageResource(R.drawable.bike);
holder.atv.setImageResource(R.drawable.atv_2);
holder.utv.setImageResource(R.drawable.utv);
if (position == 0) {
holder.text.setText("17.82 mi");
}
if (position == 1) {
holder.text.setText("29.09 mi");
}
if (position == 2) {
holder.text.setText("11.06 mi");
}
return view;
}
private void copyReadAssets() {
AssetManager assetManager =context. getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(context.getFilesDir(), ta);
try {
in = assetManager.open(ta);
out = context.openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" +context. getFilesDir() + "/"+ta),
"application/pdf");
context. startActivity(intent);
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
}
}
[CODE/]
Try below code to open PDF,
in = assetManager.open(ta);
String out= Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ ta);
File outFile = new File(out, ta);
FileOutputStream oStream = new FileOutputStream(outFile);
copyFile(in, oStream);
i want to create own image gallery with camera button..am able to show all device images in grid, and capture image also. but grid view not get refreshed dynamically..plz suggest
first i show all available images, when user capture image i want to add that image dynamically in grid view..
//--------code to get images from device--------------
private void getImages()
{
projection = new String[]{MediaStore.Images.Thumbnails._ID};
cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection,
null,
null,
MediaStore.Images.Thumbnails.IMAGE_ID);
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
}
//--------
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.e("RESULT CODE", "--" + resultCode);
if (resultCode == RESULT_OK) {
if (requestCode == CAMERA_REQUEST) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
Random random = new Random();
int no1 = random.nextInt(15 - 0);
int no2 = random.nextInt(25 - 15) + 15;
String fileName = tempString.substring(no1, no2) + ".jpg";
try {
Bitmap photo = (Bitmap) data.getExtras().get("data");
File outFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), fileName);
FileOutputStream fos = new FileOutputStream(outFile);
photo.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
getImages();
// sdcardImages.setAdapter(imageAdapter);
imageAdapter.notifyDataSetChanged();
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
//--custom adapter class
public class ImageAdapter extends BaseAdapter{
Context context;
public ImageAdapter(Context localContext){
context = localContext;}
public int getCount() {
return cursor.getCount();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
final ImageView picturesView;
if (convertView == null){
picturesView = new ImageView(context);
cursor.moveToPosition(position);
int imageID = cursor.getInt(columnIndex);
picturesView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTER
NAL_CONTENT_URI,"" + imageID));
picturesView.setScaleType(ImageView.ScaleType.FIT_XY);
picturesView.setPadding(5, 5, 5, 5);
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth() / 3;
picturesView.setLayoutParams(new GridView.LayoutParams(width, width));
} else {
picturesView = (ImageView) convertView;
}
return picturesView;
}//get view
}//image adapter
try this, I think since you are updating the image view only if convertview == null, when the view gets recycled it seems you are not updating the imageview, try replacing your getView() with this
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView pictures;
if (convertView == null)
pictures = new ImageView(context);
convertView.setTag(pictures);
} else {
pictures = (ImageView) convertView.getTag();
}
cursor.moveToPosition(position);
int imageID = cursor.getInt(columnIndex);
picturesView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
picturesView.setScaleType(ImageView.ScaleType.FIT_XY);
picturesView.setPadding(5, 5, 5, 5);
return picturesView;
}//get view
I take a photo from the camera and store them in a GridView, now when i view them there is no OnClick function to Enlarge or Goto new Activity to Enlarge the selected image, Here's the code:
Where to implement the OnClick method to take the selected image to a new Activity and enlarge it
package com.example.veeresh.myphotogallery;
public class Photo_1 extends AppCompatActivity implements OnClickListener {
Button captureBtn = null;
final int CAMERA_CAPTURE = 1;
private Uri picUri;
private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private GridView grid;
private List<String> listOfImagesPath;
private String path;
private String pathMusic;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo);
int pos = 0;
path = " ";
pathMusic = getIntent().getStringExtra("Store to Music");
if(pathMusic != null)
{
path = pathMusic;
}
captureBtn = (Button) findViewById(R.id.capture_btn1);
captureBtn.setOnClickListener(this);
grid = (GridView) findViewById(R.id.gridviewimg);
listOfImagesPath = null;
listOfImagesPath = RetriveCapturedImagePath();
if (listOfImagesPath != null) {
grid.setAdapter(new ImageListAdapter(this, listOfImagesPath));
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (arg0.getId() == R.id.capture_btn1) {
try {
//use standard intent to capture an image
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//we will handle the returned data in onActivityResult
Toast.makeText(this, "Launching Camera", Toast.LENGTH_SHORT).show();
startActivityForResult(captureIntent, CAMERA_CAPTURE);
} catch (ActivityNotFoundException anfe) {
//display an error message
String errorMessage = "Whoops - your device doesn't support capturing images!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
//user is returning from capturing an image using the camera
if (requestCode == CAMERA_CAPTURE) {
Bundle extras = data.getExtras();
Bitmap thePic = extras.getParcelable("data");
String imgcurTime = dateFormat.format(new Date());
File imageDirectory = new File(path);
imageDirectory.mkdirs();
String _path = path + imgcurTime + ".jpg";
try {
FileOutputStream out = new FileOutputStream(_path);
thePic.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.close();
} catch (FileNotFoundException e) {
e.getMessage();
} catch (IOException e) {
e.printStackTrace();
}
listOfImagesPath = null;
listOfImagesPath = RetriveCapturedImagePath();
if (listOfImagesPath != null) {
grid.setAdapter(new ImageListAdapter(this, listOfImagesPath));
}
}
}
}
private List<String> RetriveCapturedImagePath() {
List<String> tFileList = new ArrayList<String>();
File f = new File(path);
if (f.exists()) {
File[] files = f.listFiles();
Arrays.sort(files);
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (file.isDirectory())
continue;
tFileList.add(file.getPath());
}
}
return tFileList;
}
public class ImageListAdapter extends BaseAdapter {
private Context context;
private List<String> imgPic;
public ImageListAdapter(Context c, List<String> thePic) {
context = c;
imgPic = thePic;
}
public int getCount() {
if (imgPic != null)
return imgPic.size();
else
return 0;
}
//---returns the ID of an item---
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
BitmapFactory.Options bfOptions = new BitmapFactory.Options();
bfOptions.inDither = false; //Disable Dithering mode
bfOptions.inPurgeable = true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared
bfOptions.inInputShareable = true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
bfOptions.inTempStorage = new byte[32 * 1024];
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
imageView.setPadding(0, 0, 0, 0);
} else {
imageView = (ImageView) convertView;
}
FileInputStream fs = null;
Bitmap bm;
try {
fs = new FileInputStream(new File(imgPic.get(position).toString()));
if (fs != null) {
bm = BitmapFactory.decodeFileDescriptor(fs.getFD(), null, bfOptions);
imageView.setImageBitmap(bm);
imageView.setId(position);
imageView.setLayoutParams(new GridView.LayoutParams(300, 600));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fs != null) {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return imageView;
}
}
}
Use setOnItemClickListener of girdview.
grid = (GridView) findViewById(R.id.gridviewimg);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(this, "you clicked at : " + position, Toast.LENGTH_LONG).show();
// put your intent here to open activity
}
});
put this code inside your onCreate(...) method.
grid.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// do your stuff.
}
});
I'm trying to add all the images contained in a folder in my custom listview, where I show a thumbnail of the photo and a little description.
I've found a lot of examples showing images from URL or drawable folder in Android Studio, but nothing working for me that loads a picture located in /storage/emulated/0/DCIM/Camera/AAAAMMGG_HHMMSS.jpg
Here is my code - MainActivity:
import android.content.ContentValues;
public class MainActivity extends ActionBarActivity implements OnClickListener {
public int PHOTO_REQUEST_CODE = 1;
private Uri fileUri;
private ArrayAdapter<String> adapter;
TextView ceScanResults;
ImageButton btnScan;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set a toolbar to replace the action bar.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
View addButton = findViewById(mci);
ArrayList<ListItem> listData = getListData();
final ListView listView = (ListView) findViewById(R.id.custom_list);
listView.setAdapter(new CustomListAdapter(this, listData));
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int position, long id) {
ListItem newsData = (ListItem) listView.getItemAtPosition(position);
Toast.makeText(MainActivity.this, "Selected :" + " " + newsData, Toast.LENGTH_LONG).show();
}
});
initViews();
}
private ArrayList<ListItem> getListData() {
ArrayList<ListItem> listMockData = new ArrayList<ListItem>();
String[] images = getResources().getStringArray(R.array.images_array);
String[] headlines = getResources().getStringArray(R.array.headline_array);
for (int i = 0; i < images.length; i++) {
ListItem newsData = new ListItem();
newsData.setUrl(images[i]);
newsData.setHeadline(headlines[i]);
newsData.setReporterName("CE code");
newsData.setDate("28/07/2015 - 10:31");
listMockData.add(newsData);
}
return listMockData;
}
private void initViews() {
//ceScanResults = (TextView) findViewById(R.id.ceResults);
btnScan = (ImageButton) findViewById(R.id.mci);
btnScan.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void initGalleryFetchImage() {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_OK);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
Uri selectedImageUri = null;
String filePath = null;
if (requestCode == 1 && resultCode == RESULT_OK ) {
Uri selectedImage = intent.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
try {
Bitmap bmp = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(selectedImage));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
//use imageUri here to access the image
selectedImageUri = fileUri;
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT).show();
}
} else if (requestCode == 2) {
selectedImageUri = intent.getData();
}
if(selectedImageUri != null){
try {
// OI FILE Manager
String filemanagerstring = selectedImageUri.getPath();
// MEDIA GALLERY
String selectedImagePath = getPath(selectedImageUri);
if (selectedImagePath != null) {
filePath = selectedImagePath;
} else if (filemanagerstring != null) {
filePath = filemanagerstring;
} else {
Toast.makeText(getApplicationContext(), "Unknown path",
Toast.LENGTH_LONG).show();
Log.e("Bitmap", "Unknown path");
}
if (filePath != null) {
decodeFile(filePath);
} else {
// bitmap = null;
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Internal error",
Toast.LENGTH_LONG).show();
Log.e(e.getClass().getName(), e.getMessage(), e);
}
}
}
}
public String getPath(Uri uri) {
String res = null;
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(uri, proj, null, null, null);
if(cursor.moveToFirst()){;
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}
cursor.close();
return res;
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.mci) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, PHOTO_REQUEST_CODE);
String fileName = "new-photo-name.jpg";
//create parameters for Intent with filename
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,"Image captured by camera");
//imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState)
//create new Intent
startActivityForResult(intent, 1);
fileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
}
public void decodeFile(String filePath) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
try ( InputStream is = new URL(filePath).openStream() ) {
Bitmap bitmap = BitmapFactory.decodeStream( is );
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// The new size we want to scale to
final int REQUIRED_SIZE = 1024;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
}
}
And the code of my CustomListAdapter:
public class CustomListAdapter extends BaseAdapter {
private ArrayList<ListItem> listData;
private LayoutInflater layoutInflater;
public CustomListAdapter(Context context, ArrayList<ListItem> listData) {
this.listData = listData;
layoutInflater = LayoutInflater.from(context);
}
#Override
public int getCount() {
return listData.size();
}
#Override
public Object getItem(int position) {
return listData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_row_layout, null);
holder = new ViewHolder();
holder.headlineView = (TextView) convertView.findViewById(R.id.title);
holder.reporterNameView = (TextView) convertView.findViewById(R.id.reporter);
holder.reportedDateView = (TextView) convertView.findViewById(R.id.date);
holder.imageView = (ImageView) convertView.findViewById(R.id.thumbImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
ListItem newsItem = listData.get(position);
holder.headlineView.setText(newsItem.getHeadline());
holder.reporterNameView.setText("" + newsItem.getReporterName());
holder.reportedDateView.setText(newsItem.getDate());
if (holder.imageView != null) {
new ImageDownloaderTask(holder.imageView).execute(newsItem.getUrl());
}
return convertView;
}
static class ViewHolder {
TextView headlineView;
TextView reporterNameView;
TextView reportedDateView;
ImageView imageView;
}
}
My class ImageDownloaderTask:
class ImageDownloaderTask extends AsyncTask<String, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
public ImageDownloaderTask(ImageView imageView) {
imageViewReference = new WeakReference<ImageView>(imageView);
}
#Override
protected Bitmap doInBackground(String... params) {
return downloadBitmap(params[0]);
}
#Override
protected void onPostExecute(Bitmap bitmap) {
if (isCancelled()) {
bitmap = null;
}
if (imageViewReference != null) {
ImageView imageView = imageViewReference.get();
if (imageView != null) {
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
} else {
Drawable placeholder = imageView.getContext().getResources().getDrawable(R.drawable.placeholder);
imageView.setImageDrawable(placeholder);
}
}
}
}
private Bitmap downloadBitmap(String url) {
HttpURLConnection urlConnection = null;
try {
URL uri = new URL(url);
urlConnection = (HttpURLConnection) uri.openConnection();
int statusCode = urlConnection.getResponseCode();
if (statusCode != HttpStatus.SC_OK) {
return null;
}
InputStream inputStream = urlConnection.getInputStream();
if (inputStream != null) {
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
return bitmap;
}
} catch (Exception e) {
urlConnection.disconnect();
Log.w("ImageDownloader", "Error downloading image from " + url);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return null;
}
}
My ListItem.xml:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_margin="5dp"
android:contentDescription="icon"
/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</LinearLayout>
Thanks a lot for your precious support!
Greetings,
Michele.
I'm using this library to load images from an URL
https://github.com/thest1/LazyList
try using code like this in Your adapter:
File img = new File("/sdcard/image.jpg");
if(img.exists()){
Bitmap bitmap = BitmapFactory.decodeFile(img.getAbsolutePath());
holder.imageView.setImageBitmap(bitmap);
}
I have a checkbox, that when checked, makes a bitmap and then saves that bitmap to internal storage. Then, in a gridView adapter, I have it check for the bitmap from internal storage with FileInputstream.
The issue is that the checkbox's onClick method is also in a class that extends baseadapter.
With the way it is now, when I start my app, it automatically checks for the bitmap and then returns a FileNotFound exception and then the onClick of the checkbox doesn't do anything.
I thought about this and realized that the reason it checks for it is that it is creating the gridView when I first open my app (which it is supposed to). In other words, it checks for the file because it is in the getView() method of my gridView adapter:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Try to reuse the views
ImageView view = (ImageView) convertView;
// if convert view is null then create a new instance else reuse it
if (view == null) {
view = new ImageView(Context);
Log.d("GridViewAdapter", "new imageView added");
}
try {
Bitmap bitmapA = null;
FileInputStream in = Context.openFileInput("bitmapA");
bitmapA = BitmapFactory.decodeStream(in);
in.close();
/*BufferedInputStream buf = new BufferedInputStream(in);
byte[] bitMapA = new byte[buf.available()];
buf.read(bitMapA);
Bitmap bM = BitmapFactory.decodeByteArray(bitMapA, 0, bitMapA.length);
*/view.setImageBitmap(bitmapA);
if (in != null) {
in.close();
}
/*if (buf != null) {
buf.close();
}*/
} catch (Exception e) {
e.printStackTrace();
}
view.setImageResource(drawables.get(position));
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
view.setLayoutParams(new android.widget.GridView.LayoutParams(70, 70));
view.setTag(String.valueOf(position));
return view;
}
Is there a way that I can make it check internal storage ONLY IF the checkbox is checked?
Please note that the onClick method of the checkbox is in one class while I am getting the bitmap in another.
Here are my two full classes:
AppInfoAdapter (the one with the onClick method---I will only post the needed coding here):
package com.example.awesomefilebuilderwidget;
IMPORTS
public class AppInfoAdapter extends BaseAdapter {
private Context mContext;
private List<ResolveInfo> mListAppInfo;
private PackageManager mPackManager;
private List<ResolveInfo> originalListAppInfo;
private Filter filter;
private String fname;
public AppInfoAdapter(Context c, List<ResolveInfo> listApp,
PackageManager pm) {
mContext = c;
this.originalListAppInfo = this.mListAppInfo = listApp;
mPackManager = pm;
Log.d("AppInfoAdapter", "top");
}
#Override
public int getCount() {
Log.d("AppInfoAdapter", "getCount()");
return mListAppInfo.size();
}
#Override
public Object getItem(int position) {
Log.d("AppInfoAdapter", "getItem");
return mListAppInfo.get(position);
}
#Override
public long getItemId(int position) {
Log.d("AppInfoAdapter", "getItemId");
return position;
}
public static Bitmap scaleDownBitmap(Bitmap default_b, int newHeight, Context c) {
final float densityMultiplier = c.getResources().getDisplayMetrics().density;
int h= (int) (100*densityMultiplier);
int w= (int) (h * default_b.getWidth()/((double) default_b.getHeight()));
default_b=Bitmap.createScaledBitmap(default_b, w, h, true);
// TO SOLVE LOOK AT HERE:http://stackoverflow.com/questions/15517176/passing-bitmap-to-other-activity-getting-message-on-logcat-failed-binder-transac
return default_b;
}
public void SaveImage(Bitmap default_b) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 100000;
n = generator.nextInt(n);
String fname = "Image-" + n +".png";
File file = new File (myDir, fname);
Log.i("AppInfoAdapter", "" + file);
if (file.exists()) file.delete();
try {
// File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
// + "/" + fname + ".png");
FileOutputStream out = mContext.getApplicationContext().openFileOutput("bitmapA", Context.MODE_WORLD_WRITEABLE);
// FileOutputStream out = new FileOutputStream(file);
default_b.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// get the selected entry
final ResolveInfo entry = (ResolveInfo) mListAppInfo.get(position);
// reference to convertView
View v = convertView;
// inflate new layout if null
if (v == null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
v = inflater.inflate(R.layout.layout_appinfo, null);
Log.d("AppInfoAdapter", "New layout inflated");
}
// load controls from layout resources
ImageView ivAppIcon = (ImageView) v.findViewById(R.id.ivIcon);
TextView tvAppName = (TextView) v.findViewById(R.id.tvName);
TextView tvPkgName = (TextView) v.findViewById(R.id.tvPack);
final CheckBox addCheckbox = (CheckBox) v
.findViewById(R.id.addCheckbox);
Log.d("AppInfoAdapter", "Controls from layout Resources Loaded");
// set data to display
ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
tvAppName.setText(entry.activityInfo.loadLabel(mPackManager));
tvPkgName.setText(entry.activityInfo.packageName);
Log.d("AppInfoAdapter", "Data Set To Display");
addCheckbox
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (addCheckbox.isChecked()) {
System.out.println("Checked");
PackageManager pm = mContext.getPackageManager();
Drawable icon = null;
try {
icon = pm
.getApplicationIcon(entry.activityInfo.packageName);
} catch (NameNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Drawable default_icon = pm.getDefaultActivityIcon();
if (icon instanceof BitmapDrawable
&& default_icon instanceof BitmapDrawable) {
BitmapDrawable icon_bd = (BitmapDrawable) icon;
Bitmap icon_b = icon_bd.getBitmap();
BitmapDrawable default_bd = (BitmapDrawable) pm
.getDefaultActivityIcon();
Bitmap default_b = default_bd.getBitmap();
if (icon_b == default_b) {
// It's the default icon
scaleDownBitmap(default_b, 100, v.getContext());
Log.d("AppInfoAdapter", "Scale Bitmap Chosen");
SaveImage(default_b);
Log.d("AppInfoAdapter", "Scaled BM saved to External Storage");
Intent intent = new Intent(v.getContext(), GridViewAdapter.class);
// intent.hasExtra("bitmapA");
v.getContext().startActivity(intent);
Log.d("AppInfoAdapter", "Intent started to send Bitmap");
}
}
} else {
System.out.println("Un-Checked");
}
}
});
// return view
return v;
}
GridViewAdapter:
package com.example.awesomefilebuilderwidget;
IMPORTS
public class GridViewAdapter extends BaseAdapter {
private Context Context;
// Keep all Images in array list
public ArrayList<Integer> drawables = new ArrayList<Integer>();
// Constructor
public GridViewAdapter(Context c){
Context = c;
Log.d("GridViewAdapter", "Constructor is set");
drawables.add(R.drawable.pattern1);
Log.d("GridViewAdapter", "pattern1 added");
drawables.add(R.drawable.pattern2);
Log.d("GridViewAdapter", "pattern2 added");
drawables.add(R.drawable.trashcan);
Log.d("GridViewAdapter", "trashcan added");
drawables.add(R.drawable.ic_launcher);
Log.d("GridViewAdapter", "ic_launcher added");
Bitmap default_b = BitmapFactory.decodeFile("picture");
}
#Override
// How many items are in the data set represented by this Adapter
public int getCount() {
return drawables.size();
}
#Override
// Get the data item associated with the specified position in the
// data set
public Object getItem(int position) {
return drawables.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Try to reuse the views
ImageView view = (ImageView) convertView;
// if convert view is null then create a new instance else reuse it
if (view == null) {
view = new ImageView(Context);
Log.d("GridViewAdapter", "new imageView added");
}
try {
Bitmap bitmapA = null;
FileInputStream in = Context.openFileInput("bitmapA");
bitmapA = BitmapFactory.decodeStream(in);
in.close();
/*BufferedInputStream buf = new BufferedInputStream(in);
byte[] bitMapA = new byte[buf.available()];
buf.read(bitMapA);
Bitmap bM = BitmapFactory.decodeByteArray(bitMapA, 0, bitMapA.length);
*/view.setImageBitmap(bitmapA);
if (in != null) {
in.close();
}
/*if (buf != null) {
buf.close();
}*/
} catch (Exception e) {
e.printStackTrace();
}
view.setImageResource(drawables.get(position));
view.setScaleType(ImageView.ScaleType.CENTER_CROP);
view.setLayoutParams(new android.widget.GridView.LayoutParams(70, 70));
view.setTag(String.valueOf(position));
return view;
}
}
FURTHER UPDATED CODING:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Try to reuse the views
ImageView view = (ImageView) convertView;
boolean checked = (mCheckBox==null)?false:(((CheckBox) mCheckBox).isChecked());
// if convert view is null then create a new instance else reuse it
if (view == null) {
view = new ImageView(Context);
Log.d("GridViewAdapter", "new imageView added");
}
if(checked = true){
try {
Bitmap bitmapA = null;
FileInputStream in = Context.openFileInput("bitmapA");
bitmapA = BitmapFactory.decodeStream(in);
in.close();
/*BufferedInputStream buf = new BufferedInputStream(in);
byte[] bitMapA = new byte[buf.available()];
buf.read(bitMapA);
Bitmap bM = BitmapFactory.decodeByteArray(bitMapA, 0, bitMapA.length);
*/view.setImageBitmap(bitmapA);
if (in != null) {
in.close();
}
/*if (buf != null) {
buf.close();
}*/
} catch (Exception e) {
e.printStackTrace();
}}
PLUS I ADDED THIS METHOD:
public void setCheckBox(CheckBox checkbox){
mCheckBox=checkbox;
}
AND THIS VARIABLE:
CheckBox mCheckBox=null;
You dont need to listen to the onClick event in the adapter.
Instead, you can read the checkbox status.
for this, in your adapter, you add a field and a setter:
CheckBox mCheckBox=null;
public void setCheckBox( CheckBox checkbox){
mCheckBox=checkbox;
}
and then, in the getView(), you add thiss line at the begining;
boolean checked = (mCheckBox==null)?false:(((CheckBox) mCheckBox).isChecked());
UPDATE
then in your activity, i suppose you have something like
GridViewAdapter mGridViewAdapter= new GridViewAdapter(this);
so, below you have to add:
CheckBox mCheckBox = findViewById(R,id.YOURCHECKBOXID);
mGridViewAdapter.setCheckBox(mCheckBox);
And that's it!