Reputation: 1617
Below are my codes. I'm trying to retrieve images from my sd card into a gridview. I am having problems with my codes. I cant figure out how to set images into my imageview. I got the error "The method setImageResource(int) in the type ImageView is not applicable for the arguments (String)"in my ImageAdapter
public class GridViewActivity extends Activity {
private String[] mFileStrings;
private File[] listFile;
GridView grid;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set title for the GridView
setTitle("GridView");
setContentView(R.layout.grid_view);
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + "Tutorial");
if (file.isDirectory()) {
listFile = file.listFiles();
mFileStrings = new String[listFile.length];
for (int i = 0; i < listFile.length; i++) {
mFileStrings[i] = listFile[i].getAbsolutePath();
}
}
grid = (GridView) findViewById(R.id.gridview);
grid.setAdapter(new ImageAdapter(this, mFileStrings));
}
ImageAdapter
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private String[] data;
public ImageAdapter(Context c, String[] d) {
mContext = c;
data=d;
}
public int getCount() {
return data.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
// Create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // If it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(data[position]); //The method setImageResource(int) in the type ImageView is not applicable for the arguments (String)
return imageView;
}
}
MY EDITS
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView) convertView;
}
File imgFile = new File("" + data[position]);
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
imageView.setImageBitmap(myBitmap);
return imageView;
}
LOGCAT :
04-12 14:18:59.811: E/AndroidRuntime(27379): FATAL EXCEPTION: main
04-12 14:18:59.811: E/AndroidRuntime(27379): java.lang.OutOfMemoryError
04-12 14:18:59.811: E/AndroidRuntime(27379): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
04-12 14:18:59.811: E/AndroidRuntime(27379): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
04-12 14:18:59.811: E/AndroidRuntime(27379): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:389)
04-12 14:18:59.811: E/AndroidRuntime(27379): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:418)
04-12 14:18:59.811: E/AndroidRuntime(27379): at com.androidbegin.gridview.ImageAdapter.getView(ImageAdapter.java:46)
04-12 14:18:59.811: E/AndroidRuntime(27379): at android.widget.AbsListView.obtainView(AbsListView.java:2201)
04-12 14:18:59.811: E/AndroidRuntime(27379): at android.widget.GridView.onMeasure(GridView.java:1026)
04-12 14:18:59.811: E/AndroidRuntime(27379): at android.view.View.measure(View.java:12892)
04-12 14:18:59.811: E/AndroidRuntime(27379): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
04-12 14:18:59.811: E/AndroidRuntime(27379): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
04-12 14:18:59.811: E/AndroidRuntime(27379): at android.view.View.measure(View.java:12892)
04-12 14:18:59.811: E/AndroidRuntime(27379): at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
04-12 14:18:59.811: E/AndroidRuntime(27379): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
04-12 14:18:59.811: E/AndroidRuntime(27379): at android.view.View.measure(View.java:12892)
04-12 14:18:59.811: E/AndroidRuntime(27379): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
04-12 14:18:59.811: E/AndroidRuntime(27379): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
04-12 14:18:59.811: E/AndroidRuntime(27379): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2265)
04-12 14:18:59.811: E/AndroidRuntime(27379): at android.view.View.measure(View.java:12892)
04-12 14:18:59.811: E/AndroidRuntime(27379): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1624)
04-12 14:18:59.811: E/AndroidRuntime(27379): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2628)
04-12 14:18:59.811: E/AndroidRuntime(27379): at android.os.Handler.dispatchMessage(Handler.java:99)
04-12 14:18:59.811: E/AndroidRuntime(27379): at android.os.Looper.loop(Looper.java:137)
04-12 14:18:59.811: E/AndroidRuntime(27379): at android.app.ActivityThread.main(ActivityThread.java:4507)
04-12 14:18:59.811: E/AndroidRuntime(27379): at java.lang.reflect.Method.invokeNative(Native Method)
04-12 14:18:59.811: E/AndroidRuntime(27379): at java.lang.reflect.Method.invoke(Method.java:511)
04-12 14:18:59.811: E/AndroidRuntime(27379): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
04-12 14:18:59.811: E/AndroidRuntime(27379): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
04-12 14:18:59.811: E/AndroidRuntime(27379): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 1
Views: 5485
Reputation: 133560
You are setting imageView.setImageResource(data[position]) where data is array of strings. You need to decode the file path as a bitmap and then set the same to your imageview.
In your getView()
File imgFile = new File(""+data[position]);
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
Edit:
Also you can use LazyList or Universal Image Loader to display images from your sdcard. What's LazyList?. Lazy List does scaling down to reduce memory consumption. Universal Image Loader works on the same principle of LazyList but it has much more Configuration Options.
https://github.com/thest1/LazyList.
https://github.com/nostra13/Android-Universal-Image-Loader
//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE=70;
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale*=2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
If bitmaps are too large you need to scale them and then display the same in imageview. Also you should be using a view holder for smooth scrolling and performance. You should recycle bitmaps when not in use. visible views in gridview are not recycled. You can use LazyList or Universal Image Loader.
http://developer.android.com/training/improving-layouts/smooth-scrolling.html.
http://www.youtube.com/watch?v=wDBM6wVEO70. The talk is about viewholder and list view performance. The same holds good for gridview.
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html. Load bitmaps efficiently.
If you run into memory leaks use MAT Analyzer to find and fix the issue. http://www.youtube.com/watch?v=_CruQY55HOk.
Upvotes: 2
Reputation: 3485
for managing Local file Path
and Online url path
for your BaseAdpter
lazy Loading
is more helps full to See
just need care in Your BaseAdpter
about
ImageLoader imageLoader=new ImageLoader(context);
...
imageLoader.DisplayImage(url, imageView);
Upvotes: 0
Reputation: 18978
you have to do this in your adapter
File f = new File("/mnt/sdcard/photo.jpg");
ImageView mImgView1 = (ImageView)findViewById(R.id.imageView);
Bitmap bmp = BitmapFactory.decodeFile(f.getAbsolutePath());
mImgView1.setImageBitmap(bmp);
here you have to do File f = new File(""+data[position]);
Edited: You are getting OutOfMemoryError
so, i ll saggest you..
check all answer of above link.
Upvotes: 0