移动后端云平台Bmob介绍
BmobFile对象 ,然后通过该对象的getFileUrl()方法获得下载链接,最后通过链接下载。下面是一个实现了添加数据、查询数据、文件上传、文件下载、推送功能的例子:
package com.example.bombexample; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import org.json.JSONObject; import com.lidroid.xutils.HttpUtils; import com.lidroid.xutils.exception.HttpException; import com.lidroid.xutils.http.ResponseInfo; import com.lidroid.xutils.http.callback.RequestCallBack; import cn.bmob.push.BmobPush; import cn.bmob.v3.Bmob; import cn.bmob.v3.BmobInstallation; import cn.bmob.v3.BmobPushManager; import cn.bmob.v3.BmobQuery; import cn.bmob.v3.datatype.BmobFile; import cn.bmob.v3.listener.FindListener; import cn.bmob.v3.listener.GetCallback; import cn.bmob.v3.listener.GetListener; import cn.bmob.v3.listener.SaveListener; import cn.bmob.v3.listener.UploadFileListener; import android.os.Bundle; import android.os.Environment; import android.app.Activity; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.Toast; public class MainActivity extends Activity { private List<User> users=new ArrayList<User>(); private HttpUtils httpUtils=null; BmobFile bmobFile =null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Bmob.initialize(this, "3b728750d1755e56dd680fa76c4a8386"); // 使用推送服务时的初始化操作 BmobInstallation.getCurrentInstallation(this).save(); // 启动推送服务 BmobPush.startWork(this, "3b728750d1755e56dd680fa76c4a8386"); httpUtils=new HttpUtils(); } // 向服务期添加数据 public void add(View view) { for(int i=0;i<10;i++) { User user=new User(); user.setName("gaigai"+i); user.setPassword("hlb"+i); users.add(user); } for(final User user:users) { user.save(this, new SaveListener() { @Override public void onSuccess() { Toast.makeText(MainActivity.this, "添加数据成功"+user.getObjectId(), Toast.LENGTH_SHORT).show(); } @Override public void onFailure(int arg0, String arg1) { Toast.makeText(MainActivity.this, "添加数据失败", Toast.LENGTH_SHORT).show(); } }); } } // 查询服务器数据 public void query(View view) { BmobQuery<User> query=new BmobQuery<User>(); query.getObject(this,"f69efc2ca8",new GetListener<User>() { @Override public void onSuccess(User arg0) { Toast.makeText(MainActivity.this, arg0.getName()+" "+arg0.getPassword(), Toast.LENGTH_SHORT).show(); } @Override public void onFailure(int arg0, String arg1) { } }); } // 查询服务器所有数据 public void queryAll(View view) { BmobQuery<User> query=new BmobQuery<User>(); query.findObjects(this, new FindListener<User>() { @Override public void onSuccess(List<User> arg0) { Toast.makeText(MainActivity.this, arg0.size()+"", Toast.LENGTH_SHORT).show(); } @Override public void onError(int arg0, String arg1) { // TODO Auto-generated method stub } }); } // 推送功能 public void data_push(View view) { BmobPushManager bmobPush = new BmobPushManager(this); bmobPush.pushMessageAll("哈哈哈哈."); } // 文件上传 public void upload(View v) throws IOException { InputStream in=getAssets().open("byx.jpg"); File file=new File(Environment.getExternalStorageDirectory()+"/byx.jpg"); if(!file.exists()) { file.createNewFile(); } FileOutputStream out=new FileOutputStream(file); int len=0; byte []buffer=new byte[1024]; while((len=in.read(buffer))!=-1) { out.write(buffer, 0, len); } out.close(); in.close(); bmobFile = new BmobFile(file); bmobFile.upload(this, new UploadFileListener() { @Override public void onSuccess() { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, bmobFile.getFileUrl(MainActivity.this)+"", Toast.LENGTH_SHORT).show(); Log.i("Url", bmobFile.getFileUrl(MainActivity.this)); } @Override public void onFailure(int arg0, String arg1) { // TODO Auto-generated method stub Toast.makeText(MainActivity.this, "上传文件失败", Toast.LENGTH_SHORT).show(); } }); } // 文件下载 public void download(View v) { BmobQuery<Test> query=new BmobQuery<Test>(); query.getObject(this, "nt7rAAAG", new GetListener<Test>() { @Override public void onSuccess(Test arg0) { arg0.getThumbnail().getFileUrl(MainActivity.this); Log.i("url",arg0.getThumbnail().getFileUrl(MainActivity.this)); downloadPic(arg0.getThumbnail().getFileUrl(MainActivity.this),arg0.getThumbnail().getFilename()); } @Override public void onFailure(int arg0, String arg1) { // TODO Auto-generated method stub } }); } public void downloadPic(String url,String filename) { httpUtils.download(url, Environment.getExternalStorageDirectory()+"/"+filename, new RequestCallBack<File>() { @Override public void onSuccess(ResponseInfo<File> responseInfo) { Log.i("download","文件存储路径"+responseInfo.result.getPath()); } @Override public void onFailure(HttpException error, String msg) { // TODO Auto-generated method stub } }); } }
项目下载地址:http://download.csdn.net/detail/aehaojiu/8653135
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。