File.delete()方法变得无用了RecyclerView

时间:2016-08-24 10:46:27

标签: android android-recyclerview

我正在使用RecyclerView制作一个用于显示sd卡文件的应用程序,我还添加了一个删除文件的按钮。但问题是RecyclerView被删除但原始文件没有删除。它说的原因是file.delete()被忽略了。任何帮助将不胜感激。

具体代码:

holder.Delbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File file = new File(String.valueOf(trackuri));

                    file.delete();


                Toast.makeText(mContext,"Deleted the file : " , Toast.LENGTH_SHORT).show();

                mContext.getContentResolver().delete(trackuri, null,null);
                mMusic.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position,mMusic.size());


            }
        });

Adapter类的整个代码:

public class MusicAdapter  extends RecyclerView.Adapter<MusicAdapter.MusicViewHolder>  {

    Context mContext;
    List<Music> mMusic;
    BitmapDrawable mPlaceholder;
    LruCache<Long, Bitmap> mBitmapCache;
    public Uri track;

    private int selectedPosition;


    public MusicAdapter(Context context, List<Music> music) {
        mMusic = new ArrayList<>();
        if(music != null) {
            mMusic.addAll(music);
        }
        mContext = context;
        mPlaceholder = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.ic_music_note_black_48dp);
        // Get the maximum size of byte we are allowed to allocate on the VM head and convert it to bytes.
        int maxSize = (int) (Runtime.getRuntime().maxMemory() / 1024);
        // Divide the maximum size by eight to get a adequate size the LRU cache should reach before it starts to evict bitmaps.
        int cacheSize = maxSize / 8;
        mBitmapCache = new LruCache<Long, Bitmap>(cacheSize) {

            @Override
            protected int sizeOf(Long key, Bitmap value) {
                // returns the size of bitmaps in kilobytes.
                return value.getByteCount() / 1024;
            }
        };
    }
    /**
     * Adds a {@link Music} item to the Adapter.
     * @param
     */
    /**
     * Adds a {@link List} of {@link Music} to the adapters.
     * This method replaces the current music items inside of the adapter with the specified music items.
     * @param
     */
    public void clearItem() {
        mMusic.clear();
    }
    public void addItems(List<Music> music) {
        // Clear the old items. I only do this so that I don't have to do duplicating checks on the music items.
        mMusic.clear();
        // Add the new music list.
        mMusic.addAll(music);
        notifyItemRangeInserted(0, music.size());
    }
    /**
     * Clears the {@link Music} items inside of this adapter.
     */
    @Override
    public MusicViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        View v = inflater.inflate(R.layout.list_item, parent, false);
        MusicViewHolder musicViewHolder = new MusicViewHolder(v);
        return musicViewHolder;
    }

    @Override
    public void onBindViewHolder(MusicViewHolder holder, final int position) {
        final Music music = mMusic.get(position);
        holder.itemView.setLongClickable(true);
        // Check the Bitmap cache for the album art first..
        final Bitmap bitmap = mBitmapCache.get(music.getAlbumId());
        // If the bitmap is not null, then use the cached images.
        if(bitmap != null){
            holder.icon.setImageBitmap(bitmap);
        }
        else {
            // No album art could be found in the cache try reloading it.
            // In a real work example you should check that this value is not some junk value indicating that their is no album artwork.
            loadAlbumArt(holder.icon, music.getAlbumId());
        }

        holder.artist.setText(music.getArtist());
        holder.title.setText(music.getTitle());
        final Uri trackuri= ContentUris.withAppendedId(
                android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, music.getId());
        final Uri turi= ContentUris.withAppendedId(
                android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, music.getId());


        holder.button8.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {

                                                Intent intent = new Intent(mContext, Playrecord.class);

                                                intent.setData(trackuri);
                                                mContext.startActivity(intent);


                                            }
                                            });
        holder.button9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                File file = new File(String.valueOf(trackuri));

                    file.delete();


                Toast.makeText(mContext,"Deleted the file : " , Toast.LENGTH_SHORT).show();

                mContext.getContentResolver().delete(trackuri, null,null);
                mMusic.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position,mMusic.size());


            }
        });




    }




    /**
     * Helper method for asynchronously loading album art.
     * @param icon
     * @param albumId
     */
    public void loadAlbumArt(ImageView icon, long albumId) {
        // Check the current album art task if any and cancel it, if it is loading album art that doesn't match the specified album id.
        if(cancelLoadTask(icon, albumId)) {
             // There was either no task running or it was loading a different image so create a new one to load the proper image.
            LoadAlbumArt loadAlbumArt = new LoadAlbumArt(icon, mContext);
            // Store the task inside of the async drawable.
            AsyncDrawable drawable = new AsyncDrawable(mContext.getResources(), mPlaceholder.getBitmap(),loadAlbumArt);
            icon.setImageDrawable(drawable);
            loadAlbumArt.execute(albumId);
        }
    }

    /**
     * Helper method cancelling {@link LoadAlbumArt}.
     *
     * @param icon
     * @param albumId
     * @return
     */
    public boolean cancelLoadTask(ImageView icon, long albumId) {
        LoadAlbumArt loadAlbumArt = (LoadAlbumArt) getLoadTask(icon);
        // If the task is null return true because we want to try and load the album art.
        if(loadAlbumArt == null) {
            return true;
        }
        if(loadAlbumArt != null) {
            // If the album id differs cancel this task because it cannot be recycled for this imageview.
            if(loadAlbumArt.albumId != albumId) {
                loadAlbumArt.cancel(true);
                return true;
            }
        }
        return false;
    }

    /**
     * Helper method for extracting an {@link LoadAlbumArt}.
     * @param icon
     * @return
     */
    public AsyncTask getLoadTask(ImageView icon) {
        LoadAlbumArt task = null;
        Drawable drawable = icon.getDrawable();
        if(drawable instanceof AsyncDrawable) {
            task = ((AsyncDrawable) drawable).getLoadArtworkTask();
        }
        return task;
    }

    public void remove(long itemId) {
    }




    private class LoadAlbumArt extends AsyncTask<Long, Void, Bitmap> {

        // URI that points to the AlbumArt database.
        private final Uri albumArtURI = Uri.parse("content://media/external/audio/albumart");
        public WeakReference<ImageView> mIcon;
        // Holds a publicly accessible albumId to be checked against.
        public long albumId;
        public Context mContext;
        int width, height;

        public LoadAlbumArt(ImageView icon, Context context) {
            // Store a weak reference to the imageView.
            mIcon = new WeakReference<ImageView>(icon);
            // Store the width and height of the imageview.
            // This is necessary for properly scalling the bitmap.
            width = icon.getWidth();
            height = icon.getHeight();
            mContext = context;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if(isCancelled() || bitmap == null){
                return;
            }
            // Check to make sure that the imageview has not been garbage collected as well as the
            // LoadArtworkTask is the same as this one.
            if(mIcon != null && mIcon.get() != null) {
                ImageView icon = mIcon.get();
                Drawable drawable = icon.getDrawable();
                if(drawable instanceof AsyncDrawable) {
                    LoadAlbumArt task = ((AsyncDrawable) drawable).getLoadArtworkTask();
                    // Make sure that this is the same task as the one current stored inside of the ImageView's drawable.
                    if(task != null && task == this) {
                        icon.setImageBitmap(bitmap);
                    }
                }
            }
            mBitmapCache.put(albumId, bitmap);
            super.onPostExecute(bitmap);
        }

        @Override
        protected Bitmap doInBackground(Long... params) {
            // AsyncTask are not guaranteed to start immediately and could be cancelled somewhere in between calling doInBackground.
            if(isCancelled()){
                return null;
            }
            albumId = params[0];
            // Append the albumId to the end of the albumArtURI to create a new Uri that should point directly to the album art if it exist.
            Uri albumArt = ContentUris.withAppendedId(albumArtURI, albumId);
            Bitmap bmp = null;

            return bmp;
        }
    }
    /**
     * Custom drawable that holds a LoadArtworkTask
     */

    private static class AsyncDrawable extends BitmapDrawable {
        WeakReference<LoadAlbumArt> loadArtworkTaskWeakReference;

        public AsyncDrawable(Resources resources, Bitmap bitmap, LoadAlbumArt task) {
            super(resources, bitmap);
            // Store the LoadArtwork task inside of a weak reference so it can still be garbage collected.
            loadArtworkTaskWeakReference = new WeakReference<LoadAlbumArt>(task);
        }

        public LoadAlbumArt getLoadArtworkTask() {
            return loadArtworkTaskWeakReference.get();
        }
    }

    @Override
    public int getItemCount() {
        return mMusic.size();
    }

    /**
     * Custom ViewHolder that represents the List Item.
     */
    public static class MusicViewHolder extends RecyclerView.ViewHolder {

        ImageView icon;
        TextView title;
        TextView artist;
        Button button8;
        Button button9;

        public MusicViewHolder(View itemView) {
            super(itemView);
            icon = (ImageView) itemView.findViewById(R.id.icon);
            title = (TextView) itemView.findViewById(R.id.title);
            artist = (TextView)itemView.findViewById(R.id.subtitle);
            button8 = (Button) itemView.findViewById(R.id.button8);
            Delbutton = (Button) itemView.findViewById(R.id.button9);
        }


    }
}

2 个答案:

答案 0 :(得分:1)

我认为问题在于你的文件对象所以在执行删除语句之前检查文件是否存在

if(file.exists)
{
 file.delete();
}

答案 1 :(得分:0)

我的问题在这里非常愚蠢,因为@Selvin指出我传递的是Uri而不是文件路径,但是在放置文件删除代码时也出现了错误,它应该放在getContentResolver代码之后,更正后的代码是:

 holder.Delbutton.setOnClickListener(new View.OnClickListener() {
            @Override
                public void onClick(View v) {


                mContext.getContentResolver().delete(trackuri, null,null);
                mMusic.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position,mMusic.size());
                File file = new File( Environment.getExternalStorageDirectory().getAbsolutePath()+"/Voico/"+music.getTitle()+".mp3");
                if(file.exists()) {
                    file.delete();

                    Toast.makeText(mContext,"Deleted the file : " , Toast.LENGTH_SHORT).show();

                }
            }
        });