Я загружаю изображения из галереи телефона и загружаю их в галерею приложений, но при каждом запуске приложения оно принудительно закрывается.
Мой код:
public class ImageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.imagelayout);
Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView parent,
View v, int position, long id)
{
Toast.makeText(getBaseContext(),
"pic" + (position + 1) + " selected",
Toast.LENGTH_SHORT).show();
}
});
}
}
public class ImageLoader {
private List<String> ImageFiles = new ArrayList<String>();
private static final String CAMERA_IMAGE_BUCKET_NAME = Environment.getExternalStorageDirectory().toString()+ "/DCIM/Camera";
private File file;
public ImageLoader(){
ReadImages();
}
public List<String> getImageFiles(){
return ImageFiles;
}
private void ReadImages(){
file = new File(CAMERA_IMAGE_BUCKET_NAME);
File[] URIs = file.listFiles(new ImageFileFilter());
for(int x = 0; x < URIs.length; x++){
File file = URIs[x];
ImageFiles.add(file.getPath());
}
}
/**
* Image File Filter Class
*/
public class ImageFileFilter implements FileFilter{
private final String[] AndroidExtensions = new String[] {"jpg", "png"};
public boolean accept(File pathname) {
for (String extension : AndroidExtensions)
{
if (pathname.getName().toLowerCase().endsWith(extension))
{
return true;
}
}
return false;
}
}
}
public class ImageAdapter extends BaseAdapter {
private Context context;
private int itemBackground;
List<String> images;
ImageLoader loader;
public ImageAdapter(Context c){
loader = new ImageLoader();
images = loader.getImageFiles();
context = c;
//---setting the style---
TypedArray a = c.obtainStyledAttributes(R.styleable.gallery);
itemBackground = a.getResourceId(
R.styleable.gallery_android_galleryItemBackground, 0);
a.recycle();
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return images.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
try{
Bitmap bm = BitmapFactory.decodeFile(images.get(position).toString());
imageView.setImageBitmap(bm);
}
catch(Exception e){
Log.e("DEBUGTAG", "Remtoe Image Exception", e);
}
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
Спасибо за помощь!
Stack trace:
Thread [<1> main] (Suspended (exception OutOfMemoryError))
BitmapFactory.decodeFile(String, BitmapFactory$Options) line: 277
BitmapFactory.decodeFile(String) line: 296
ImageAdapter.getView(int, View, ViewGroup) line: 63
Gallery.makeAndAddView(int, int, int, boolean) line: 745
Gallery.fillToGalleryRight() line: 697
Gallery.layout(int, boolean) line: 628
Gallery.onLayout(boolean, int, int, int, int) line: 336
Gallery(View).layout(int, int, int, int) line: 7035
LinearLayout.setChildFrame(View, int, int, int, int) line: 1249
LinearLayout.layoutVertical() line: 1125
LinearLayout.onLayout(boolean, int, int, int, int) line: 1042
LinearLayout(View).layout(int, int, int, int) line: 7035
LinearLayout.setChildFrame(View, int, int, int, int) line: 1249
LinearLayout.layoutHorizontal() line: 1238
LinearLayout.onLayout(boolean, int, int, int, int) line: 1044
LinearLayout(View).layout(int, int, int, int) line: 7035
FrameLayout.onLayout(boolean, int, int, int, int) line: 333
FrameLayout(View).layout(int, int, int, int) line: 7035
PhoneWindow$DecorView(FrameLayout).onLayout(boolean, int, int, int, int) line: 333
PhoneWindow$DecorView(View).layout(int, int, int, int) line: 7035
ViewRoot.performTraversals() line: 1045
ViewRoot.handleMessage(Message) line: 1727
ViewRoot(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 4627
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 858
ZygoteInit.main(String[]) line: 616
NativeStart.main(String[]) line: not available [native method]