編輯:關於Android編程
private final static String mbApiKey = "CuOLkaVfoz1zGsqFKDgfvI0h"; private final static String mbRootPath = "/apps/wp2pcs";3、app首次運行進行登錄時,要把你的帳號授權給wp2pcs應用,以便創建目錄、讀寫文件等操作;下面是百度pcs的登錄授權頁面截圖
import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.baidu.oauth.BaiduOAuth; import com.baidu.oauth.BaiduOAuth.BaiduOAuthResponse; import com.baidu.pcs.BaiduPCSActionInfo; import com.baidu.pcs.BaiduPCSActionInfo.PCSCommonFileInfo; import com.baidu.pcs.BaiduPCSActionInfo.PCSFileFromToInfo; import com.baidu.pcs.BaiduPCSActionInfo.PCSFileFromToResponse; import com.baidu.pcs.BaiduPCSActionInfo.PCSFileInfoResponse; import com.baidu.pcs.BaiduPCSActionInfo.PCSListInfoResponse; import com.baidu.pcs.BaiduPCSActionInfo.PCSQuotaResponse; import com.baidu.pcs.BaiduPCSActionInfo.PCSSimplefiedResponse; import com.baidu.pcs.BaiduPCSClient; import com.baidu.pcs.BaiduPCSStatusListener; import com.example.exmnetdisk.R; import com.example.exmnetdisk.pcs.ConfirmDialogFragment.ConfirmCallbacks; import com.example.exmnetdisk.pcs.InputDialogFragment.InputCallbacks; public class PCSActivity extends Activity implements OnClickListener ,OnItemClickListener,OnItemLongClickListener,ConfirmCallbacks,InputCallbacks { private static final String TAG = "PCSActivity"; private TextView tv_desc; private TextView tv_path; private ListView lv_content; private Button btn_login; private Button btn_logout; private Button btn_upload; private Button btn_rename; private Button btn_back; private Button btn_create; private String mbOauth = null; private final static String mbApiKey = "CuOLkaVfoz1zGsqFKDgfvI0h"; private final static String mbRootPath = "/apps/wp2pcs"; private String mCurrentPath = mbRootPath; private String mLocalPath; private Handler mbUiThreadHandler = new Handler(); private final static String POSITION = "position"; private final static String OPERATION = "operation"; private final static String FILEPATH = "file_path"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_pcs); tv_desc = (TextView)findViewById(R.id.tv_desc); tv_path = (TextView)findViewById(R.id.tv_path); lv_content = (ListView)findViewById(R.id.lv_content); lv_content.setOnItemClickListener(this); lv_content.setOnItemLongClickListener(this); btn_login = (Button)findViewById(R.id.btn_login); btn_logout = (Button)findViewById(R.id.btn_logout); btn_upload = (Button)findViewById(R.id.btn_upload); btn_rename = (Button)findViewById(R.id.btn_rename); btn_back = (Button)findViewById(R.id.btn_back); btn_create = (Button)findViewById(R.id.btn_create); btn_login.setOnClickListener(this); btn_logout.setOnClickListener(this); btn_upload.setOnClickListener(this); btn_rename.setOnClickListener(this); btn_back.setOnClickListener(this); btn_create.setOnClickListener(this); mLocalPath = Environment.getExternalStorageDirectory().toString()+"/Download"; } @Override public void onClick(View v) { if (v.getId() == R.id.btn_login) { login(); } else if (v.getId() == R.id.btn_logout) { logout(); } else if (v.getId() == R.id.btn_upload) { upload("2.txt"); } else if (v.getId() == R.id.btn_rename) { rename(); } else if (v.getId() == R.id.btn_back) { if (null != mbOauth) { if (mCurrentPath.equals(mbRootPath)) { showTip("當前已是根目錄"); } else { mCurrentPath = mCurrentPath.substring(0, mCurrentPath.lastIndexOf("/")); getList(mCurrentPath); } } } else if (v.getId() == R.id.btn_create) { InputDialogFragment dialog = InputDialogFragment.newInstance("請輸入目錄名稱"); String fragTag = getResources().getString(R.string.app_name); dialog.show(getFragmentManager(), fragTag); } } private void showTip(String message) { Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show(); } //登錄 private void login() { BaiduOAuth oauthClient = new BaiduOAuth(); oauthClient.startOAuth(this, mbApiKey, new String[]{"basic", "netdisk"}, new BaiduOAuth.OAuthListener() { @Override public void onException(String msg) { showTip("Login failed " + msg); } @Override public void onComplete(BaiduOAuthResponse response) { if (null != response) { mbOauth = response.getAccessToken(); showTip("Token: " + mbOauth + " User name:" + response.getUserName()); tv_desc.setText("當前登錄用戶是"+response.getUserName()+",token為"+mbOauth); getQuota(); getList(mbRootPath); } } @Override public void onCancel() { showTip("Login cancelled"); } }); } //獲取磁盤空間 private void getQuota() { if (null != mbOauth) { Thread workThread = new Thread(new Runnable() { @Override public void run() { BaiduPCSClient api = new BaiduPCSClient(); api.setAccessToken(mbOauth); final PCSQuotaResponse info = api.quota(); mbUiThreadHandler.post(new Runnable() { public void run() { if (null != info) { if (0 == info.status.errorCode) { showTip("Quota :" + info.total + " used: " + info.used); tv_desc.setText(tv_desc.getText()+"\n網盤總容量是"+info.total/1024/1024+"M,已經使用"+info.used/1024/1024+"M."); } else { showTip("Quota failed: " + info.status.errorCode + " " + info.status.message); } } } }); } }); workThread.start(); } } //注銷 private void logout() { if (null != mbOauth) { Thread workThread = new Thread(new Runnable() { @Override public void run() { BaiduOAuth oauth = new BaiduOAuth(); final boolean ret = oauth.logout(mbOauth); mbUiThreadHandler.post(new Runnable() { @Override public void run() { showTip("Logout " + ret); } }); } }); workThread.start(); } } private ListmFileList = new ArrayList (); //獲取文件/目錄列表 private void getList(final String path) { if (null != mbOauth) { Thread workThread = new Thread(new Runnable() { @Override public void run() { BaiduPCSClient api = new BaiduPCSClient(); api.setAccessToken(mbOauth); final PCSListInfoResponse ret = api.list(path, "name", "asc"); mbUiThreadHandler.post(new Runnable() { public void run() { if (ret.status.errorCode == 0) { mFileList.clear(); mFileList = ret.list; FileListAdapter adapter = new FileListAdapter(getApplicationContext(), mFileList); lv_content.setAdapter(adapter); tv_path.setText("當前目錄是"+mCurrentPath); } else { tv_path.setText("獲取目錄"+mCurrentPath+"失敗,原因是"+ret.status.message); } } }); } }); workThread.start(); } } @Override public void onItemClick(AdapterView parent, View view, int position, long id) { PCSCommonFileInfo file = mFileList.get(position); if (file.isDir == true) { mCurrentPath = file.path; showTip("您點擊了目錄"+mCurrentPath); getList(mCurrentPath); } else { ConfirmDialogFragment fsf = ConfirmDialogFragment.newInstance( R.drawable.ic_launcher, "下載確認", "是否下載文件"+file.path+"?"); Map map_param = new HashMap (); map_param.put(OPERATION, "download"); map_param.put(POSITION, position); map_param.put(FILEPATH, file.path); fsf.setParam(map_param); fsf.show(getFragmentManager(), ""); } } @Override public boolean onItemLongClick(AdapterView parent, View view, int position, long id) { PCSCommonFileInfo file = mFileList.get(position); String desc = String.format("是否刪除%s%s?", file.isDir?"目錄":"文件", file.path); ConfirmDialogFragment fsf = ConfirmDialogFragment.newInstance( R.drawable.ic_launcher, "刪除確認", desc); Map map_param = new HashMap (); map_param.put(OPERATION, "delete"); map_param.put(POSITION, position); map_param.put(FILEPATH, file.path); fsf.setParam(map_param); fsf.show(getFragmentManager(), ""); return true; } @Override public void onConfirmSelect(Map map_param) { String operation = (String) map_param.get(OPERATION); if (operation.equals("delete")) { int position = (Integer) map_param.get(POSITION); PCSCommonFileInfo file = mFileList.get(position); deletePCSFile(file.path); } else if (operation.equals("download")) { String file = (String) map_param.get(FILEPATH); download(file); } } @Override public void onInput(String dir) { createDir(dir); } //創建目錄 private void createDir(final String dir) { if (null != mbOauth) { Thread workThread = new Thread(new Runnable() { @Override public void run() { BaiduPCSClient api = new BaiduPCSClient(); api.setAccessToken(mbOauth); String path = mCurrentPath + "/" + dir; final PCSFileInfoResponse ret = api.makeDir(path); mbUiThreadHandler.post(new Runnable() { public void run() { showTip("Mkdir: " + ret.status.errorCode + " " + ret.status.message); getList(mCurrentPath); } }); } }); workThread.start(); } } //刪除文件。注意不能用deleteFile這個函數名,因為deleteFile在ContextWrapper類中已經定義 private void deletePCSFile(final String file_path) { if (null != mbOauth) { Thread workThread = new Thread(new Runnable() { @Override public void run() { BaiduPCSClient api = new BaiduPCSClient(); api.setAccessToken(mbOauth); List files = new ArrayList (); files.add(file_path); final PCSSimplefiedResponse ret = api.deleteFiles(files); mbUiThreadHandler.post(new Runnable() { public void run() { showTip("Delete files: " + ret.errorCode + " " + ret.message); getList(mCurrentPath); } }); } }); workThread.start(); } } private BaiduPCSStatusListener mStatusListener = new BaiduPCSStatusListener() { @Override public void onProgress(long bytes, long total) { final long bs = bytes; final long tl = total; mbUiThreadHandler.post(new Runnable() { public void run() { showTip("total: " + tl + " loaded:" + bs); } }); } @Override public long progressInterval() { return 500; } }; //下載文件 private void download(final String file) { if (null != mbOauth) { final String local_file = String.format("%s/%s", mLocalPath, file.substring(1+file.lastIndexOf("/"))); Thread workThread = new Thread(new Runnable() { @Override public void run() { BaiduPCSClient api = new BaiduPCSClient(); api.setAccessToken(mbOauth); final PCSSimplefiedResponse ret = api.downloadFileFromStream(file, local_file, mStatusListener); mbUiThreadHandler.post(new Runnable() { public void run() { Log.d(TAG, "Download files: " + ret.errorCode + " " + ret.message); showTip("download: " + ret.errorCode + " " + ret.message); } }); } }); workThread.start(); } } // 上傳文件 private void upload(String file) { if (null != mbOauth) { final String local_file = String.format("%s/%s", mLocalPath, file); final String remote_file = String.format("%s/%s", mCurrentPath, file); Thread workThread = new Thread(new Runnable() { @Override public void run() { BaiduPCSClient api = new BaiduPCSClient(); api.setAccessToken(mbOauth); final PCSFileInfoResponse response = api.uploadFile(local_file, remote_file, mStatusListener); mbUiThreadHandler.post(new Runnable() { public void run() { showTip("upload: " + response.status.errorCode + " " + response.status.message + " " + response.commonFileInfo.blockList); getList(mCurrentPath); } }); } }); workThread.start(); } } //文件改名 private void rename() { if (null != mbOauth) { Thread workThread = new Thread(new Runnable() { @Override public void run() { BaiduPCSClient api = new BaiduPCSClient(); api.setAccessToken(mbOauth); List info = new ArrayList (); PCSFileFromToInfo data1 = new PCSFileFromToInfo(); data1.from = mCurrentPath + "/2.txt"; data1.to = "1.txt"; info.add(data1); Log.d(TAG, "from="+data1.from+",to="+data1.to); final PCSFileFromToResponse ret = api.rename(info); mbUiThreadHandler.post(new Runnable() { public void run() { showTip("Rename: " + ret.status.errorCode + " " + ret.status.message); getList(mCurrentPath); } }); } }); workThread.start(); } } }
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import com.alibaba.sdk.android.oss.ClientConfiguration; import com.alibaba.sdk.android.oss.OSS; import com.alibaba.sdk.android.oss.OSSClient; import com.alibaba.sdk.android.oss.common.OSSLog; import com.alibaba.sdk.android.oss.common.auth.OSSCredentialProvider; import com.alibaba.sdk.android.oss.common.auth.OSSPlainTextAKSKCredentialProvider; import com.example.exmnetdisk.R; public class AliyunActivity extends Activity { private OSS oss; // 以下字段為注冊賬號並開通oss服務後由阿裡雲自動分配 private static final String endpoint = "http://oss-cn-shanghai.aliyuncs.com"; private static final String accessKeyId = ""; private static final String accessKeySecret = ""; private static final String testBucket = ""; // 需付費購買存儲空間,才能正常使用上傳、下載、列表等功能 private static final String uploadFilePath = " "; //上傳的目錄 private static final String uploadObject = "sampleObject"; //上傳的文件 private static final String downloadObject = "sampleObject"; //下載的文件 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_aliyun); OSSCredentialProvider credentialProvider = new OSSPlainTextAKSKCredentialProvider(accessKeyId, accessKeySecret); ClientConfiguration conf = new ClientConfiguration(); conf.setConnectionTimeout(15 * 1000); // 連接超時,默認15秒 conf.setSocketTimeout(15 * 1000); // socket超時,默認15秒 conf.setMaxConcurrentRequest(5); // 最大並發請求書,默認5個 conf.setMaxErrorRetry(2); // 失敗後最大重試次數,默認2次 OSSLog.enableLog(); oss = new OSSClient(getApplicationContext(), endpoint, credentialProvider, conf); // 上傳 Button upload = (Button) findViewById(R.id.upload); upload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { new PutObjectSamples(oss, testBucket, uploadObject, uploadFilePath).asyncPutObjectFromLocalFile(); } }).start(); } }); // 下載 Button download = (Button) findViewById(R.id.download); download.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { new GetObjectSamples(oss, testBucket, downloadObject).asyncGetObjectSample(); } }).start(); } }); // 羅列 Button list = (Button) findViewById(R.id.list); list.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { new ListObjectsSamples(oss, testBucket).asyncListObjectsWithPrefix(); } }).start(); } }); // 管理 Button manage = (Button) findViewById(R.id.manage); manage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { new ManageObjectSamples(oss, testBucket, uploadObject).headObject(); } }).start(); } }); // 分塊上傳 Button multipart = (Button) findViewById(R.id.multipart); multipart.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { try { new MultipartUploadSamples(oss, testBucket, uploadObject, uploadFilePath).multipartUpload(); } catch (Exception e) { e.printStackTrace(); } } }).start(); } }); // 斷點續傳 Button resumable = (Button) findViewById(R.id.resumable); resumable.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { new ResuambleUploadSamples(oss, testBucket, uploadObject, uploadFilePath).resumableUpload(); } }).start(); } }); // 簽名URL Button sign = (Button) findViewById(R.id.sign); sign.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { new SignURLSamples(oss, testBucket, uploadObject, uploadFilePath).presignConstrainedURL(); } }).start(); } }); // bucket管理 Button bucket = (Button) findViewById(R.id.bucket); bucket.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { new Thread(new Runnable() { @Override public void run() { new ManageBucketSamples(oss, testBucket, uploadFilePath).deleteNotEmptyBucket(); } }).start(); } }); } }
Android系統默認的Toast十分簡潔,使用也非常的簡單。但是有時我們的程序使用默認的Toast時會和程序的整體風格不搭配,這個時候我們就需要自定義Toast,使其與
一、Android平台濾鏡濾鏡這個功能在目前的市場上應用很廣泛,發展也非常快,總結起來,基本上有以下三種應用會包含濾鏡功能,都各有所長。 二、相機濾鏡介紹1、相
時隔5個月時間,即6月14日,紅米3S作為紅米3的升級版,配備了高通骁龍430八核處理器,配備4100毫安時電池,定價699元起。紅米3S相對於紅米3的售價
我有一篇文章很不負責的,沒有頭緒的分析了一些Volley的源碼。我自己回頭去看了一下,於是就把他刪掉了,於是就有了今天的這篇文章。Volley的使用步驟創建一個Reque