日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

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

發(fā)布時間:2024/9/27 Android 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android 聊天背景图片,Android 实现从本地读取图片更改聊天背景 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

現(xiàn)在很多社交軟件都有這個功能,因為本次我參加一個比賽也是要做一個社交軟件,所以我就“畫蛇添足”的添加了這個一個功能,因為我也是個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" />

以上就是布局了 然后就是實現(xiàn)代碼

3.首先我們說下思路,點擊 聊天背景 按鈕 -->調用系統(tǒng)的圖庫 -->選擇圖片-->讀取圖片的路徑 -->在確定是否設置為背景圖片的界面上顯示該圖片 -->點擊是或否-->是則將布局文件背景修改,否則關閉當前acticity回到適當位置

以下代碼為是否確認使用布局文件關聯(lián)的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);

//第一個調用系統(tǒng)圖庫

//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.然后再在自己定義的聊天界面上 把靜態(tài)的圖片地址path字符串和 布爾型的靜態(tài)iscash傳到實現(xiàn)聊天界面的類中 進行操作

這里貼出主要代碼

if(ChatBackground.isCash){

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

}

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

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

注意:好像是Android 4.3嗎什么的 修改了讀取文件路徑的那種宏,我也不是很懂那個,反正就是讀取文件和獲取文件路徑的方法有點不一樣了。

以上

總結

以上是生活随笔為你收集整理的android 聊天背景图片,Android 实现从本地读取图片更改聊天背景的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。