请问在activity的android:background属性能不能设置为外部存储的图片,比如SD卡上的,android:background="/mnt/sdcard/Image/Main/main_bg.bmp",我是在activity的layout中的FrameLayout设置的。好像运行的时候会报错,找不到文件。我再代码里面可以打开这个文件,证明路径是对的。但是android貌似不认,难道只能添加到res里面,再作为背景吗?背景图
解决方案 »
- android 平台 实现类似excel 的同时锁定行和列的效果
- android上实现人脸识别的功能
- 求教Android 将APK安装到/system/app的方法
- android apk键入信息点击按钮调用另一个apk并向另一个apk里头传递参数
- 调用了addView以后,发现getHeight的数据还是没add之间的!!
- SDK自带的ApiDemo缺少R.java!!
- 触屏问题
- 请问下如何用真手机做模拟器?
- 在listview点击item改变imageview的状态
- Spinner 选择不同项,显示不同样式
- 问个可能比较2的问题。。。怎么能不让我的程序每次打开都要改IP地址。。。
- 【快马加鞭】 急求!!android ndk 使用 fstream 在编译时总是no file find。
background.java:package com.amaker.Sprite;import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.Serializable;import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import org.xmlpull.v1.XmlPullParserException;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.provider.MediaStore;
import android.util.Log;
import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.ViewTreeObserver.OnPreDrawListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.Gallery.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewSwitcher.ViewFactory;@SuppressWarnings("serial")
public class BackGroundActivity extends Activity implements ViewFactory,OnItemSelectedListener,OnTouchListener,OnGestureListener,Serializable{
/*共享数据类*/
private ClassShared shared;
SharedPreferences preferences;
SharedPreferences.Editor editor;
private static final String TAG = "BackGroundActivity";
/*webservice测试*/
private Button btnWebServiceTest;
private TextView txtResultOfWebService;
private SoapObject detail; /*布局成员*/
private RelativeLayout layout_left;
private LinearLayout layout_right;
private ImageButton btnChangeMode;
private TextView mtextView;
private ImageButton btnChangeBackGround;
private ImageButton btnIntnetSearch;
private RelativeLayout bglayout;
private Gallery g;
private LyricView lyricView;
private ViewTreeObserver viewTreeObserver_left;
private ViewTreeObserver viewTreeObserver_right;
@Override
protected void onCreate(Bundle savedInstanceState){
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.background);
/*初始化布局*/
InitLayout();
/*设置事件监听*/
}
public void InitLayout(){
/*获取布局ID*/
btnWebServiceTest = (Button) findViewById(R.id.btnWebServiceTest);
txtResultOfWebService = (TextView)findViewById(R.id.txtOfWebService);
layout_right = (LinearLayout) findViewById(R.id.layout_right);
layout_left = (RelativeLayout) findViewById(R.id.layout_left);
bglayout = (RelativeLayout)findViewById(R.id.backgroundlayout);
btnChangeBackGround = (ImageButton)findViewById(R.id.btnChangeBackGround);
btnIntnetSearch = (ImageButton)findViewById(R.id.btnIntnetSearch);
lyricView = (LyricView)findViewById(R.id.mylrc);
btnChangeMode = (ImageButton)findViewById(R.id.btnChangeMode);
mtextView = (TextView)findViewById(R.id.textViewMode);
g = (Gallery) findViewById(R.id.gallery);
//显示布局
/*设置可监听触摸手势的控件*/
mGestureDetector=new GestureDetector(this);
layout_left.setOnTouchListener(this);
layout_right.setOnTouchListener(this);
lyricView.setOnTouchListener(this);
lyricView.setLongClickable(true);
bglayout.setOnTouchListener(this);
bglayout.setLongClickable(true);
/*初始化共享数据类*/
shared = (ClassShared)getApplication();
/*preferences设置播放模式*/
Context ctx = BackGroundActivity.this;
preferences = ctx.getSharedPreferences("11",MODE_WORLD_READABLE);/*获取只能被本程序读写的sharedpreferences对象*/
editor = preferences.edit();/*获取修改器*/
String backgroundId = preferences.getString("backgroundId", "");
if(backgroundId.equals("")){
layout_left.setBackgroundResource(R.drawable.pic8);
}
else{
try {
Uri uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,backgroundId+"");
Bitmap bm = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
layout_left.setBackgroundDrawable(new BitmapDrawable(bm));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String modeText = preferences.getString("modeType","列表循环ing");
mtextView.setText(modeText);
}
btnChangeBackGround.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Log.i(TAG,"换背景 -----");
shared.GetPicture();
SetGallary();
}
});
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
} //layout_right
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Uri uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,shared.mPicList.get(arg2).get("PicId")+"");
try {
Bitmap bm = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
layout_left.setBackgroundDrawable(new BitmapDrawable(bm));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
editor.putString("backgroundId",shared.mPicList.get(arg2).get("PicId").toString());
editor.commit();
}
public void SetGallary()
{
g.setAdapter(new ImageAdapter(this));
g.setOnItemSelectedListener(this);
g.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
}
});
}
public class ImageAdapter extends BaseAdapter{
private Context mContext;
public ImageAdapter(Context c){
mContext = c;
}
public int getCount(){
//Log.i(TAG,"----mPicList.size() ="+Integer.toString(mPicList.size()));
return shared.mPicList.size();
}
public Object getItem(int position)
{
return position;
}
public long getItemId(int position)
{
return position;
}
public View getView(int position,View convertView,ViewGroup parent){
ImageView i = new ImageView(mContext);
Uri uri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,shared.mPicList.get(position).get("PicId")+"");
Bitmap bm;
try {
bm = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
i.setImageBitmap(bm);
i.setAdjustViewBounds(true);
i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}
}
@Override
public View makeView() {
// TODO Auto-generated method stub
ImageView i = new ImageView(this);
i.setScaleType(ImageView.ScaleType.FIT_CENTER);
i.setLayoutParams(new ImageSwitcher.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
return i;
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
2.background.xml文件,只保留了图片读取相关的控件:<?xml version="1.0" encoding="utf-8" ?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/backgroundlayout" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">
<ImageButton android:id="@+id/btnChangeBackGround" android:layout_below="@+id/btnChangeMode" android:layout_alignParentLeft="true" android:src="@drawable/rest" android:layout_width="wrap_content" android:layout_height="wrap_content" />
<Gallery android:id="@+id/gallery"
android:background="#55000000"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true"
android:layout_height="60dip"
android:spacing="16dp"
android:gravity="center_vertical"
/>
</RelativeLayout>
这是我程序里面弄出来的,有这些代码应该可以写出来了。希望有用。
1.先让BackGroundActivity类implements相应的接口
2.实现接口相应的函数。
需要的接口我都已经保留下来了。
还忘了给你一个函数了:
先是存储图片的链表://图片库,用于存储图片
public List<HashMap<String,Object>> mPicList = new ArrayList<HashMap<String,Object>>();然后是从SD卡读取图片的函数:public void GetPicture(){
Cursor cursor=null;
try{
cursor = this.getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, null, null, null,null);
cursor.moveToFirst();
String picUrl=null;//路径
String picName=null;//标题
int picId = 0;//ID
while(cursor!=null&&cursor.moveToNext())
{
picUrl= cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
picId = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
HashMap<String,Object> map = new HashMap<String,Object>();
map.put("PicUrl",picUrl);
map.put("PicId",picId);
mPicList.add(map);
}
}finally{
if(cursor!=null){
try{
cursor.close();
}catch(Exception e){
}
}
}
}
不能再详细了。