代码在android中的设备上无效

时间:2012-12-27 06:30:05

标签: android android-gallery

我想将存储的图像从SD卡中的AutoistDiary文件夹显示到图库中, 活动开始于我已编写该代码的模拟器中,但是当我在我的设备上执行相同的apk时,它会强制关闭,

这是我的代码,通过强制关闭实际设备, 在这种情况下该怎么做?

//get the gallery view---------------------------------------------------------------------        
         Gallery g = (Gallery) findViewById(R.id.gallery);  
            g.setAdapter(new ImageAdapter(this, SDCard()));           
            g.setOnItemClickListener(new OnItemClickListener() 
            {  
                public void onItemClick(AdapterView<?> parent,  View v, int position, long id) 
                {  

                    Log.i("position", Integer.toString(position));
                }  
            });  




    }
    private List<String> SDCard()  
    {  
        Log.i("inside", "SDCard() method");
     List<String> tFileList = new ArrayList<String>();  
     File dir = Environment.getExternalStorageDirectory();
//   File yourFile = new File(dir, "path/to/the/file/inside/the/sdcard.ext");
     //It have to be matched with the directory in SDCard  
     File f = new File( dir, "/AutoistDiary/");  
    //File f = new File(  "/sdcard/");
     File[] files=f.listFiles();  

     for(int i=0; i<files.length; i++)  
     {  
      File file = files[i];  
      /*It's assumed that all file in the path 
        are in supported type*/  
      tFileList.add(file.getPath());

      msg="getting autioistdiary file path" + i+tFileList;
      showToastMessage(msg);

     }  

     return tFileList;  
    } 

    public class ImageAdapter extends BaseAdapter 
    {  
        int mGalleryItemBackground;  
        private Context mContext;  
        private List<String> FileList;  

        public ImageAdapter(Context c, List<String> fList) 
        {  
            mContext = c;  
            FileList = fList;  
            TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);  
            mGalleryItemBackground = a.getResourceId(  
            R.styleable.Gallery1_android_galleryItemBackground,0);  
            a.recycle();  
        }  

        public int getCount() {  
            return FileList.size();  
        }  

        public Object getItem(int position) {  
            return position;  
        }   

        public long getItemId(int position) {  
            return position;  
        }  

        public View getView(int position, View convertView,  
          ViewGroup parent) {  
            ImageView i = new ImageView(mContext);  

            Bitmap bm = BitmapFactory.decodeFile(FileList.get(position).toString());  
            i.setImageBitmap(bm);  

            i.setLayoutParams(new Gallery.LayoutParams(100,70));  
            i.setScaleType(ImageView.ScaleType.FIT_XY);  
            i.setBackgroundResource(mGalleryItemBackground);  

            Log.i("images ", "added to gallery from card");
            msg="added to gallery from card";
            showToastMessage(msg);
            return i;  
        }  
    }  
    public TypedArray obtainStyledAttributes(int theme) {  
        // TODO Auto-generated method stub  
        return null;  
    } 

这是我的logcat     12-27 12:02:42.016:I / inside(1217):docsshow 12-27 12:02:42.056:D / szipinf(1217):初始化膨胀状态12-27 12:02:42.086:D / szipinf(1217) ):初始化膨胀状态12-27 12:02:42.216:I / inside(1217):SDCard()方法12-27 12:02:42.996:D / skia(1217):--- SkImageDecoder :: Factory返回null 12-27 12:02:43.017:I / images(1217):从卡片12-27添加到图库12:02:43.866:D / skia(1217):--- SkImageDecoder :: Factory返回null 12-27 12 :02:43.886:I / images(1217):

1 个答案:

答案 0 :(得分:2)

我浏览了你的代码,可能有两个问题,一个是没有获得SDCard路径,返回Bitmap为null,或者图像是两个大的显示。 希望这会对你有所帮助

if (requestCode == ACTIVITY_SELECT_IMAGE)
      {
       if (resultCode == Activity.RESULT_OK) 
        {
            Uri selectedImage = data.getData();
            galleryImatePath = getRealPathFromURI(selectedImage);

            Bitmap bitmapThumbnail = null;

            BitmapFactory.Options bfOptions =new BitmapFactory.Options();
            try 
            {

                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.inSampleSize=5;
                bfOptions.inTempStorage=new byte[32 * 1024];
                //highImageReso.setVisibility(View.VISIBLE);
                imgbutCross.setVisibility(View.VISIBLE);
                imgbutPriview.setVisibility(View.VISIBLE);

                /**
                 * Algorithm for uploading picture with low resolution                  
                 */
                InputStream stream;
                try {
                    stream = getContentResolver().openInputStream(selectedImage);
                    myImage = BitmapFactory.decodeStream(stream, null , bfOptions);
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                myImage.compress(Bitmap.CompressFormat.JPEG, 100, bos);
                bitmapdata = bos.toByteArray();

                /**
                 * Algorithm for selected image preview on Add Note Popup                   
                 */
                InputStream streamFromLocation = null;
                try {
                    streamFromLocation = getContentResolver().openInputStream(selectedImage);
                    bitmapThumbnail = decodeSampledBitmapFromResource(streamFromLocation, 120, 120, selectedImage);

                } catch (Exception e) {
                    e.printStackTrace();
                }
                imgbutPriview.setImageBitmap(bitmapThumbnail);
                isPhoto = true;

            } 
            catch (OutOfMemoryError e)
            {
                System.out.println("OutofMemoryException------------------"+e.toString());
                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.inSampleSize=8;
                bfOptions.inTempStorage=new byte[32 * 1024];

                try {
                    InputStream stream;
                    try {
                        stream = getContentResolver().openInputStream(selectedImage);
                        myImage = BitmapFactory.decodeStream(stream, null , bfOptions);
                    } catch (Exception e2) {
                        // TODO Auto-generated catch block
                        e2.printStackTrace();
                    }
                } catch (OutOfMemoryError e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }

                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                myImage.compress(Bitmap.CompressFormat.JPEG, 100, bos);
                bitmapdata = bos.toByteArray();

                /**
                 * Algorithm for selected image preview on Add Note Popup                   
                 */
                InputStream streamFromLocation = null;
                try {
                    streamFromLocation = getContentResolver().openInputStream(selectedImage);
                    bitmapThumbnail = decodeSampledBitmapFromResource(streamFromLocation, 120, 120, selectedImage);

                } catch (Exception e2) {
                    e2.printStackTrace();
                }
                imgbutPriview.setImageBitmap(bitmapThumbnail);

                isPhoto = true;
            }
            catch(Exception e)
            {

            }
        } 
      }