GridView Android上传图片

时间:2015-09-29 17:05:27

标签: android gridview

我从图库中的gridView上传了一些图片,一切正常,直到现在。我从GridView中删除了我想要的图像,一切正常,但是如果我想删除照片后再次上传,在我的gridView中,会显示我刚删除的相同图像,我不知道怎么办解决这个。如果我重新启动我的整个应用程序一切正常,但如果我再次删除一些图片,再次是同样的问题,如果我重新启动只有这个活动没有任何改变。这是我的onActivityResult后用户做ACTION_PICK:< / p>

 public void onActivityResult(int requestCode, int resultCode, Intent data) {


    // get image from external storage

    if (resultCode == RESULT_OK) {
        if (requestCode == REQUEST_CODE_EXTERNAL_IMAGE) {
            Uri uri = data.getData();
            InputStream inputStream;



                OutputStream output;
                // Find the SD Card path
                File filepath = Environment.getExternalStorageDirectory();

                int count2 = 0;
                SharedPreferences settings = getSharedPreferences("YOUR_PREF_NAME", 0);
                count2 = settings.getInt("SNOW_DENSITY", 0); //0 is the default value
                String paranteza1 = "(";
                String paranteza2 = ")";
                int count1 = new File("/mnt/sdcard" + nameAlbum).listFiles().length;
                count1 = count1 + 1;
                String nameImage = "Wallpaper_" + count1 + ".jpg";
                for (int j = 0; j < listFile.length; j++) {
                    FileNameStrings[j] = listFile[j].getName();


                    if (FileNameStrings[j].equals(nameImage)) {
                        nameImage = "Wallpaper_" + count1 + paranteza1 + count2 + paranteza2 + ".jpg";
                        count2++;
                        settings = getSharedPreferences("YOUR_PREF_NAME", 0);
                        SharedPreferences.Editor editor = settings.edit();
                        editor.putInt("SNOW_DENSITY", count2);
                        editor.commit();
                        for (int i = 0; i < listFile.length; i++) {
                            FileNameStrings[i] = listFile[i].getName();
                            if (FileNameStrings[i].equals(nameImage)) {

                                nameImage = "Wallpaper_" + count1 + paranteza1 + count2 + paranteza2 + ".jpg";
                                count2++;
                                settings = getSharedPreferences("YOUR_PREF_NAME", 0);
                                editor = settings.edit();
                                editor.putInt("SNOW_DENSITY", count2);
                                editor.commit();
                            }
                        }
                    }
                }
                count2++;
                File dir = new File(filepath.getAbsolutePath()
                        + nameAlbum);

                // Retrieve the image from the res folder


                // Create a name for the saved image
                file = new File(dir, nameImage);


                try {
                    inputStream = getContentResolver().openInputStream(uri);
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    output = new FileOutputStream(file);

                    // Compress into png format image from 0% - 100%
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, output);
                    output.flush();
                    output.close();

                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

        }


        if (!Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            Toast.makeText(this, "Error! No SDCARD Found!", Toast.LENGTH_LONG)
                    .show();
        } else {
            // Locate the image folder in your SD Card
            file = new File(Environment.getExternalStorageDirectory()
                    + File.separator + nameAlbum);

            if (file.isDirectory()) {
                listFile = file.listFiles();
                // Create a String array for FilePathStrings
                FilePathStrings = new String[listFile.length];
                // Create a String array for FileNameStrings
                FileNameStrings = new String[listFile.length];

                for (int i = 0; i < listFile.length; i++) {
                    // Get the path of the image file
                    FilePathStrings[i] = listFile[i].getAbsolutePath();
                    // Get the name image file
                    FileNameStrings[i] = listFile[i].getName();
                    // Locate the GridView in gridview_main.xml
                    grid = (GridView) findViewById(R.id.gridview);
                    // Pass String arrays to LazyAdapter Class
                    adapter = new GridViewAdapter(AlbumActivity.this, FilePathStrings, FileNameStrings);
                    // Set the LazyAdapter to the GridView
   grid.setAdapter(adapter);
                    adapter.notifyDataSetChanged();


                }
            }
        }

这是我的代码,允许我删除文件

  case R.id.action_delete:

            File filepath = Environment.getExternalStorageDirectory();

            SparseBooleanArray checkedItemPositions = grid.getCheckedItemPositions();

            int itemCount = grid.getCount();


            for (int i = itemCount - 1; i >= 0; i--) {
                if (checkedItemPositions.get(i)) {

                    File dir5 = new File(filepath.getAbsolutePath()
                            + nameAlbum + FileNameStrings[i]);

                    File file3 = new File(String.valueOf(dir5));
                    deleted = file3.delete();
                    deleteCache(this);



                }

1 个答案:

答案 0 :(得分:0)

我一直在为自己解决这个问题,在我删除照片之后,当我上传我的照片时,我在我的目录中使用相同的名称上传,并且GridView采用了相同的图像,但仅限于对于imageview,如果II点击图片,我会被发送到另一个活动,在那里显示好照片。无论如何,现在总是当我上传新的照片时,我上传它们,用其他名字,没有名字是一样的!!!对于像我这样有同样问题的人,我认为这是一个很好的建议!