2016-08-23 10 views
0

私は、RecyclerViewを使ってsdカードからファイルを表示するためのアプリケーションを作成しています。ファイルを削除するボタンも追加しました。しかし、問題はRecyclerViewが削除されますが、元のファイルは削除されません。どんな助けもありがとう。ファイルが削除されない - RecyclerView

OnClick削除ボタンのコード:

holder.deleteButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       File file = new File(String.valueOf(trackuri)); 
       file.delete(); 
       mMusic.remove(position); 
       notifyItemRemoved(position); 
       notifyItemRangeChanged(position,mMusic.size()); 



       Toast.makeText(mContext,"Removed : " , Toast.LENGTH_SHORT).show(); 

      } 
     }); 

完全なクラスアダプタのコード:

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(); 
      mMusic.remove(position); 
      notifyItemRemoved(position); 
      notifyItemRangeChanged(position,mMusic.size()); 



      Toast.makeText(mContext,"Removed : " , Toast.LENGTH_SHORT).show(); 

     } 
    }); 




} 




/** 
* 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 deleteButton; 

    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); 
     deleteButton = (Button) itemView.findViewById(R.id.deleteButton); 
    } 


} 

}

+0

いただきましOnBindViewHolderクラスのアダプタクラスでtrackuri' –

+0

'の値 –

+0

'最終ウリtrackuri = ContentUris.withAppendedId( android.provider.MediaStore.Audio。 Media.EXTERNAL_CONTENT_URI、music.getId()); ' –

答えて

1

この

mContext.getContentResolver().delete(trackuri,null, null); 

ように削除更新:file.delete();の使用これだけではない

mContext.getContentResolver().delete(trackuri,null, null); 

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

これはビューのキャッシュからそれを削除しますが、デバイスを再起動するとすべて元の状態に戻ります。ファイルがsdcardから削除されない –

+0

チェックのアップデートセクション –

+0

は同じですが、ファイルマネージャ –

関連する問題