简体   繁体   English

Android中的SD卡

[英]SD card in Android

In my app I have two buttons play and download. 在我的应用程序中,有两个按钮可以播放和下载。 In the downlod button I download video from internet and store in SD card, i will play video from the SD card when press play button. 在向下按钮中,我从互联网上下载视频并存储在SD卡中,当按下播放按钮时,我将从SD卡中播放视频。

Video is successfully downloaded and stored in SD card. 视频已成功下载并存储在SD卡中。 If I press play button, I will list videos from SD card(in logcat ) and play the downloaded video. 如果按播放按钮,我将列出SD卡中的视频(在logcat )并播放下载的视频。 It does not show the downloaded video name, but if I open the SD card from my system the downloaded video is stored in SD card. 它不显示下载的视频名称,但是如果我从系统中打开SD卡,则下载的视频将存储在SD卡中。 i do not know where i am wrong. 我不知道我在哪里错。

You have to add media files to Media Store in order to be seen by gallery widget. 您必须将媒体文件添加到“媒体商店”,才能被图库窗口小部件看到。 Use MediaScanner. 使用MediaScanner。 I use this convenient wrapper in my code: 我在代码中使用了这个方便的包装器:

public class MediaScannerWrapper implements  
MediaScannerConnection.MediaScannerConnectionClient {
    private MediaScannerConnection mConnection;
    private String mPath;
    private String mMimeType;

    // filePath - where to scan; 
    // mime type of media to scan i.e. "image/jpeg". 
    // use "*/*" for any media
    public MediaScannerWrapper(Context ctx, String filePath, String mime){
        mPath = filePath;
        mMimeType = mime;
        mConnection = new MediaScannerConnection(ctx, this);
    }

    // do the scanning
    public void scan() {
        mConnection.connect();
    }

    // start the scan when scanner is ready
    public void onMediaScannerConnected() {
        mConnection.scanFile(mPath, mMimeType);
        Log.w("MediaScannerWrapper", "media file scanned: " + mPath);
    }

    public void onScanCompleted(String path, Uri uri) {
        // when scan is completes, update media file tags
    }
}

Then instantiate MediaScannerWrapper and start it with scan() . 然后实例化MediaScannerWrapper并使用scan()启动它。 You could tweak it to handle more than one file at the time. 您可以对其进行调整以同时处理多个文件。 Hint: pass List of File paths, and then loop around mConnection.scanFile . 提示:传递文件路径列表,然后在mConnection.scanFile周围循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM