1

我如何在 gridview 和选中的选中图像上添加复选框将被删除?在这段代码中,我该如何修改请帮助我,以及如何在网格中显示图像它自己的图像而不是图标 ido 做什么?

     GridView gridView;
   TextView textView;
    File currentParent;
   File[] currentFiles;
  String[] currentFilePath;
  ImageButton AddPictures;
  SimpleAdapter simpleAdapter;
  final Context context = this;

  DataBase db;

  ProgressDialog myProgressDialog = null;

  File root;

Handler handle = new Handler(){ 
    public void handleMessage(android.os.Message msg) {

        if (msg.what == 1)
        {
            hideProgress();

            //list.setAdapter(new  
   AppPhotosAdapter(getApplicationContext(),activity,0,picList));
        }
        else if (msg.what == 2)
        {
            hideProgress();
        }
        super.handleMessage(msg);   
    };
};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    File photos = new File(getFilesDir(),"photos");
    photos.mkdirs();

    File root1 = new File("/data/data/com.newsoftwares.folderlock/files/");

    currentParent = root1;
    currentFiles = root1.listFiles();

    currentFilePath = new String[currentFiles.length];
    int count = 0;

    for(File f:currentFiles)
    {   
        currentFilePath[count] = f.getAbsolutePath();
        count++;
    }


    gridView =(GridView)findViewById(R.id.grid);
    gridView.setOnItemClickListener(new OnItemClickListener(){
        public void onItemClick(AdapterView<?> parent, View view, int 
     position,long id) {


        if(currentFiles[position].isDirectory())
        {
            root = new File("/data/data/com.myexample

/files/"+FileName(currentFilePath[位置])+"/");

            Log.e("Root first",root+ " ");

            currentFiles = root.listFiles();

            inflateListView(currentFiles);
        }
        else if(currentFiles[position].isFile())
        {
            db = new DataBase(getBaseContext()); 
            try {
                db.createDataBase();
            } catch (IOException e1) {

                e1.printStackTrace();
            }

            Log.e("Check name",currentFiles[position].getName()+" ");

            Cursor DataC = db.selectQuery("SELECT path FROM Photos where name 
='"+currentFiles[position].getName()+"'");

            Log.e("Cursor",DataC.getCount()+" ");

            if(DataC.getCount() > 0)
            {
                Bitmap bitmap = decodeFile(new 
 File(root+"/"+currentFiles[position].getName()));

                try {
                    Log.e("DB Path",DataC.getString(DataC.getColumnIndex("path"))+ " ");

                    FileOutputStream outputStream = new FileOutputStream(new File(DataC.getString(DataC.getColumnIndex("path"))));
                    outputStream.write(getBitmapAsByteArray(bitmap));
                    outputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                Log.e("Delete path 
 ",root+"/"+currentFiles[position].getName()+ " ");

                     File file = new 
      File(root+"/"+currentFiles[position].getName());
                     file.delete();

                     inflateListView(currentFiles);


            }

            DataC.close();
            db.close();
        }
    }

  });


    inflateListView(currentFiles);  
 }

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);

        //The new size we want to scale to
        final int REQUIRED_SIZE=70;

        //Find the correct scale value. It should be the power of 2.
        int scale=1;
        while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
            scale*=2;

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}
    return null;
 }

 public byte[] fileToByte(File f)
 {
    byte[] byteArray = null;
    try
    {
         InputStream inputStream = new FileInputStream(f);
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
         byte[] b = new byte[1024*8];
         int bytesRead =0;

         while ((bytesRead = inputStream.read(b)) != -1)
         {
             bos.write(b, 0, bytesRead);
         }

         byteArray = bos.toByteArray();
     }
     catch (IOException e)
     {
         e.printStackTrace();
     }

     return byteArray;
  }
 ///////////////////// Get File Name from path ////////////////////////////
 public String FileName(String path)
    {
    String f = " /";
    boolean c = false;

    for(int i=path.length()-1;i>0;i--)
    {
        if(c == false)
            if(path.charAt(i) == f.charAt(1))
            {
                c = true;
                return 
       path.substring(i+1,path.length());       
            }
    }

    return "";
}

  public static byte[] getBitmapAsByteArray(Bitmap bitmap) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0, outputStream);       
    return outputStream.toByteArray();
}

 private void showProgress()
{
    myProgressDialog = ProgressDialog.show(FileGridActivity.this,null,   
         "Loading...", true); 
}

private void hideProgress()
{
    if (myProgressDialog != null)
        myProgressDialog.dismiss();
}

   private void inflateListView(File[] files){

List<Map<String,Object>> listItems = new ArrayList<Map<String,Object>>();

 for(int i=0;i<files.length;i++)
 {      
        Map<String, Object> listItem = new HashMap<String, Object>();

        if(files[i].isDirectory())
        {
            listItem.put("icon", R.drawable.folder);
        }
        else
        {
            listItem.put("icon", R.drawable.file);
        }

        listItem.put("fileName", files[i].getName());
        listItems.add(listItem);
    }

    simpleAdapter=new SimpleAdapter(this,listItems,R.layout.line,new String[]
   {"icon","fileName"},new int[]{R.id.icon,R.id.file_name});
    gridView.setAdapter(simpleAdapter);


   }
4

0 回答 0