android 聊天背景图片,Android 实现从本地读取图片更改聊天背景

现在很多社交软件都有这个功能,因为本次我参加一个比赛也是要做一个社交软件,所以我就“画蛇添足”的添加了这个一个功能,因为我也是个Android初学者,所以说修改bug浪费了我至少15个小时,简直是苦逼。

废话不多少 开始;

首先 :需要的是聊天界面 ,本次不予讨论 聊天背景我是设在一个relativelayout里面 所以我只贴出这个布局的xml

1:聊天背景的XML (部分)

xmlns:android="http://schemas.android.com/apk/res/android"

android:id="@+id/backgroundRL"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:background="#eee">

2:我设置了一个确定界面 ,就是选着图片的时候确定是否是这张 这里也给出代码<?xml version="1.0" encoding="utf-8"?>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

android:background="#eee" >

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="center_vertical"

android:background="#eee">

android:id="@+id/backgroundOK"

android:layout_width="100sp"

android:layout_height="wrap_content"

android:background="@drawable/btn_style_green"

android:text="确认"/>

android:id="@+id/backgroundNO"

android:layout_width="100sp"

android:layout_height="wrap_content"

android:background="@drawable/btn_style_white"

android:layout_toRightOf="@id/backgroundOK"

android:text="取消"

android:onClick="backgroundNO"/>

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_gravity="center_vertical">"

android:id="@+id/backgroundIV"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#eee" />

以上就是布局了 然后就是实现代码

3.首先我们说下思路,点击 聊天背景 按钮 -->调用系统的图库 -->选择图片-->读取图片的路径 -->在确定是否设置为背景图片的界面上显示该图片 -->点击是或否-->是则将布局文件背景修改,否则关闭当前acticity回到适当位置

以下代码为是否确认使用布局文件关联的java类

package com.feng.decipherstranger;

import java.io.FileNotFoundException;

import java.io.IOException;

import android.R.bool;

import android.R.string;

import android.annotation.SuppressLint;

import android.app.Activity;

import android.content.ContentResolver;

import android.content.Intent;

import android.content.res.Resources;

import android.database.Cursor;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.drawable.BitmapDrawable;

import android.graphics.drawable.Drawable;

import android.net.Uri;

import android.os.Bundle;

import android.provider.MediaStore;

import android.provider.MediaStore.Images.Media;

import android.util.Log;

import android.view.LayoutInflater;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.EditText;

import android.widget.ImageView;

import android.widget.RelativeLayout;

import android.widget.RelativeLayout.LayoutParams;

import android.widget.Toast;

public class ChatBackground extends Activity{

public static boolean isCash = false;

public static String path;

private final int RESULT_LOAD_IMAGE = 1;

private static final String IMAGE_TYPE = "image/*";

private ImageView backgroundIV;

private Button backgrooundYES;

private String iscashS;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.nothing);

//第一个调用系统图库

//Intent intent = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

intent.setType(IMAGE_TYPE);

startActivityForResult(intent,RESULT_LOAD_IMAGE);

//同时关闭nothing 不然会引起必须点击两次返回

//this.finish();

}

//重写onActivityResult方法

@SuppressLint("NewApi")

public void onActivityResult(int requestCode, int resultCode, Intent data){

super.onActivityResult(requestCode,resultCode,data);

if(requestCode==RESULT_LOAD_IMAGE&&resultCode==RESULT_OK&&data!=null){

try {

Uri selectImage = data.getData();

ContentResolver resolver = getContentResolver();

Bitmap bm = MediaStore.Images.Media.getBitmap(resolver,selectImage);

String[] filePathColumn = {MediaStore.Images.Media.DATA};

Cursor cursor = resolver.query(selectImage,

filePathColumn, null, null, null);

int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

cursor.moveToFirst();

final String picturePath = cursor.getString(columnIndex);

path = picturePath;

//将选取的图片文件指定为聊天背景

backgroundIV = (ImageView)findViewById(R.id.backgroundIV);

backgroundIV.setBackground(Drawable.createFromPath(picturePath));

} catch (FileNotFoundException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

} catch (IOException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

backgrooundYES = (Button)findViewById(R.id.backgroundOK);

backgrooundYES.setOnClickListener(new OnClickListener() {

@Override

public void onClick(View v) {

// TODO 自动生成的方法存根

isCash = true;

/*if (isCash) {

iscashS = "iscash is true";

}

Log.i(iscashS,path);*/

//提示

Toast.makeText(getApplicationContext(),"修改成功",Toast.LENGTH_SHORT).show();

finish();

}

});

}

else {

Toast.makeText(getApplicationContext(),"没有选择图片",Toast.LENGTH_SHORT).show();

}

}

public void backgroundNO(View v){//取消 方法

this.finish();

}

}

4.然后再在自己定义的聊天界面上 把静态的图片地址path字符串和 布尔型的静态iscash传到实现聊天界面的类中 进行操作

这里贴出主要代码

if(ChatBackground.isCash){

//Log.i("chatactivity test",ChatBackground.path);

}

backgroundRL = (RelativeLayout)findViewById(R.id.backgroundRL);

backgroundRL.setBackground(Drawable.createFromPath(ChatBackground.path));

注意:好像是Android 4.3吗什么的 修改了读取文件路径的那种宏,我也不是很懂那个,反正就是读取文件和获取文件路径的方法有点不一样了。

以上

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值