android设备SD卡文件扫描与同步(暂备份)

package com.owo.contentresolvermedia;


import java.io.File;
import java.util.ArrayList;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.FileObserver;
import android.provider.MediaStore;
import android.support.v4.widget.CursorAdapter;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity {


private static final String TAG = "MainActivity";
private MainFrame mMainFrame;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mMainFrame = new MainFrame(this);
setContentView(mMainFrame);
}


@Override
protected void onDestroy() {
mMainFrame.onDestroy();
super.onDestroy();
}


public class MainFrame extends LinearLayout {


// click this button to create video file onto file system
private Button mAddVideoButton;
// show all the video
private ListView mVideoListView;
// video list adapter
private VideoCursorAdapter mVideoListAdapter;


public MainFrame(Context context) {
super(context);


initComponents();
setupListener();
onLanguageChanged();
startWatch();
}


private void initComponents() {
mAddVideoButton = new Button(getContext());
mVideoListView = new ListView(getContext());


mVideoListAdapter = new VideoCursorAdapter(getContext(), query());
mVideoListView.setAdapter(mVideoListAdapter);


setOrientation(VERTICAL);
addView(mAddVideoButton);
addView(mVideoListView);
}


private void setupListener() {
// change path value to the source file path of your device.
// every time you click the #mAddVideoButton, a copy of this file
// is created in the same parent folder
final String src = Environment.getExternalStorageDirectory() + File.separator + "a.mp4";
mAddVideoButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
final String dest = Environment.getExternalStorageDirectory() + File.separator
+ System.currentTimeMillis() + ".mp4";
Utils.copyFile(src, dest);
Toast.makeText(getContext(), "Succeed to make new file [" + dest + "]",
Toast.LENGTH_SHORT).show();
}
});


mFileObserver.startWatching();
}


FileObserver mFileObserver = new FileObserver(Environment.getExternalStorageDirectory()
.getAbsolutePath()) {
// private ArrayList<String> mBuffer = new ArrayList<String>();


@Override
public void onEvent(int event, String path) {
path = "file://" + Environment.getExternalStorageDirectory() + File.separator
+ path;
switch (event) {
case FileObserver.MOVED_FROM:
Log.d(TAG, "MOVED_FROM:" + path);
break;
case FileObserver.MOVED_TO:
Log.d(TAG, "MOVED_TO:" + path);
break;
case FileObserver.CREATE:
Log.d(TAG, "CREATE:" + path);


// solution 1
{
Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
scanIntent.setData(Uri.parse(path));
getContext().sendBroadcast(scanIntent);
}


// solution 2: does not work
// {
// mBuffer.add(path);
// String[] arrayStrings = new String[mBuffer.size()];
// mBuffer.toArray(arrayStrings);
// mBuffer.clear();
// MediaScannerConnection.scanFile(
// getContext(),
// arrayStrings,
// new String[] {
// MimeTypeMap.getSingleton().getMimeTypeFromExtension(
// "mp4") },
// new MediaScannerConnection.OnScanCompletedListener() {
// @Override
// public void onScanCompleted(String path, Uri uri) {
// Log.v(TAG, "OnScanCompleted");
// }
// });
// }
break;
case FileObserver.DELETE:
Log.d(TAG, "DELETE:" + path);
break;
case FileObserver.DELETE_SELF:
Log.d(TAG, "DELETE_SELF:" + path);
break;
case FileObserver.MOVE_SELF:
Log.d(TAG, "MOVE_SELF:" + path);
break;
default:
break;
}
}
};


private void onLanguageChanged() {
mAddVideoButton.setText("Add a video file");
}


public void onDestroy() {
mFileObserver.stopWatching();
}


/**
* <pre>
* Sync logic:
* 1. read data from #ContentResolver first to initialize UI
* 2. send re-scan broadcast to scan the file-system to obtain the newest media list
* 3. register file system #FileObserver to observe file system change and compute 
*    which folder need to be scanned
* </pre>
*/


// private Handler mHandler = new Handler();
// private Runnable mTimerTask = new Runnable() {
// @Override
// public void run() {
// AccountManager am = AccountManager.get(getContext());
// Account[] accounts = am.getAccounts();
//
// for (Account account : accounts) {
// int isSyncable = ContentResolver.getIsSyncable(account,
// MediaStore.AUTHORITY);
//
// if (isSyncable > 0) {
// Bundle extras = new Bundle();
// extras.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true);
// ContentResolver.requestSync(accounts[0], MediaStore.AUTHORITY,
// extras);
// }
// }
//
// mHandler.postDelayed(this, 1000 * 10);
// }
// };


private void startWatch() {
// AccountManager am = AccountManager.get(getContext());
// Account[] accounts = am.getAccounts();
//
// for (Account account : accounts) {
// Bundle extras = new Bundle();
// ContentResolver.setIsSyncable(account, MediaStore.AUTHORITY, 1);
// ContentResolver.setSyncAutomatically(account,
// MediaStore.AUTHORITY, true);
// ContentResolver.addPeriodicSync(account, MediaStore.AUTHORITY,
// extras, 10);
// }
// ContentResolver.addStatusChangeListener(ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS
// | ContentResolver.SYNC_OBSERVER_TYPE_PENDING
// | ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE, new
// SyncStatusObserver() {
// @Override
// public void onStatusChanged(int which) {
// switch (which) {
// case ContentResolver.SYNC_OBSERVER_TYPE_SETTINGS:
// Log.v(TAG, "SYNC_OBSERVER_TYPE_SETTINGS");
// break;
// case ContentResolver.SYNC_OBSERVER_TYPE_PENDING:
// Log.v(TAG, "SYNC_OBSERVER_TYPE_PENDING");
// break;
// case ContentResolver.SYNC_OBSERVER_TYPE_ACTIVE:
// Log.v(TAG, "SYNC_OBSERVER_TYPE_ACTIVE");
// break;
// default:
// break;
// }
// }
// });
}


private class VideoCursorAdapter extends CursorAdapter {
public VideoCursorAdapter(Context context, Cursor c) {
super(context, c);
}


@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView dest = (TextView) view;
dest.setText(Utils.getColumn(cursor, MediaStore.Video.Media.TITLE));
}


@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return new TextView(context);
}
}


private Cursor query() {
final String[] videoColumns = new String[] { MediaStore.Video.Media._ID,//
MediaStore.Video.Media.DATA, //
MediaStore.Video.Media.TITLE, };


return getContext()
.getApplicationContext()
.getContentResolver()
.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videoColumns, null, null,
null);


}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值