Bmob平台实现头像上传+Volley框架加载图片url

这段时间项目中有用到第三方云推送服务,看了看网络上比较好的云平台,觉得Bmob(http://www.bmob.cn/)还是不错的,拥有自己的体系和接口,开发文档很全面。推荐给开发者使用。先上图

Bmob云的头像上传:

Bmobfile中的方法--upload,回调两个值给开发者,success和failed,其中包含图片url,根据返回的url使用volley进行图片的简单加载即可。

首先,基本的布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.ynu.demoforvolley.MainActivity">

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#EC3C3D"
        android:onClick="Camera"
        android:textColor="#fff"
        android:text="拍一张呗"/>

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/show"/>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#EC3C3D"/>
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#EC3C3D"
        android:layout_marginTop="10dp"
        android:onClick="Download"
        android:textColor="#fff"
        android:text="图片下载"/>
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/down"/>
</LinearLayout>
MainActivity.java

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.Volley;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.listener.UploadFileListener;

public class MainActivity extends AppCompatActivity {

    private static int Camera_Request_Code=1;
    ImageView imageView,imageView1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView= (ImageView) findViewById(R.id.show);//显示拍摄图片
        imageView1= (ImageView) findViewById(R.id.down);//显示下载图片
        Bmob.initialize(this,"0d04c904eb8bda07fe4fdcdee059dca1");	//这里改成你的开发者tag
    }

    public void Camera(View v){
        Log.i("-----打开摄像头-----","摄像头启动");
        Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent,Camera_Request_Code);//根据不同请求返回,这里=Camera_Request_Code=1时返回
    }

    public void Download(View v){
        Log.i("-----准备下载-----","点击事件开始");
        ImageRequest imageRequest=new ImageRequest(
                "http://file.bmob.cn/M03/6D/CB/oYYBAFc3PdqAZkSXAAAEowi_LOQ331.png",
                new Response.Listener<Bitmap>() {
                    @Override
                    public void onResponse(Bitmap response) {
                        imageView1.setImageBitmap(response);
                    }
                }, 0, 0, Bitmap.Config.RGB_565, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                imageView.setImageResource(R.mipmap.ic_launcher);//失败用这张图片
            }
        });
        RequestQueue mQueue = Volley.newRequestQueue(MainActivity.this);//创建一个volley队列
        mQueue.add(imageRequest);//加入队列 开始下载
        Toast.makeText(MainActivity.this, "下载成功", Toast.LENGTH_SHORT).show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==Camera_Request_Code){
            Log.i("-----返回成功-----","摄像头启动");
            if (data==null){
                Log.i("-----返回数据空-----","用户没有进行拍照");
                return;
            }else {
                Bundle result=data.getExtras();
                if (result!=null){
                    Bitmap bm=result.getParcelable("data");//拿到数据存入bm
                    Uri uri=BitMap(bm);
                    imageView.setImageBitmap(bm);
                    Toast.makeText(MainActivity.this, "拍摄成功!", Toast.LENGTH_SHORT).show();
                    String path = "sdcard/Bmobupload.png";
                    final BmobFile file=new BmobFile(new File(path));
                    file.upload(MainActivity.this, new UploadFileListener() {
                        @Override
                        public void onSuccess() {
                            Log.i("-----上传成功-----","到Bmob文件管理中去看");
                            String imgUrl=file.getFileUrl(MainActivity.this);
                            Toast.makeText(MainActivity.this, "上传成功"+"这是对应的url"+imgUrl , Toast.LENGTH_SHORT).show();
                        }

                        @Override
                        public void onFailure(int i, String s) {
                            Toast.makeText(MainActivity.this, "上传失败,失败原因:"+s , Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        }
    }

    //存放图片的地址,可以改动
    private Uri BitMap(Bitmap bitmap){
        File tmpDir=new File(Environment.getExternalStorageDirectory()+"/Bmob");    //保存地址及命名
        if (!tmpDir.exists()){
            tmpDir.mkdir(); //生成目录进行保存
        }
        File img=new File(tmpDir.getAbsolutePath()+"upload.png");
        try {
            FileOutputStream fos=new FileOutputStream(img);
            bitmap.compress(Bitmap.CompressFormat.PNG, 85, fos);  //参:压缩的格式,图片质量85,输出流
            fos.flush();
            fos.close();
            return Uri.fromFile(img);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}

相信我的注释已经够详细了,不算复杂,不要忘记添加网络权限和Bmob的SDK及Volley框架的jar包等基本要素。

Demo整理好了给大家发出来。

  • 7
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值