我正在尝试在编辑文本中显示图像和文字,但它不适用于冰淇淋三明治和Jellybean .. 它在以下版本中运行良好。
当活动暂停并再次恢复时,它会在编辑文本中显示图像名称而不是图像以及文本..
请参阅图片及相关代码
public class MainActivity extends Activity implements OnClickListener,
OnItemClickListener {
String path = Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/Emoticons";
HashMap<String, Integer> emoticons = new LinkedHashMap<String, Integer>();
ArrayList<String> arrayList = new ArrayList<String>();
Bitmap bitmap;
EditText editText;
ImageButton cat_one;
ArrayList<String> name = new ArrayList<String>();
View view;
CharSequence[] namesList = { "Email"};
Dialog dialog;
GridView gridView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
File sd = new File(path);
if (!sd.exists())
sd.mkdirs();
}
addComponents();
addListeners();
}
private void fillArrayList() {
Iterator<Entry<String, Integer>> iterator = emoticons.entrySet()
.iterator();
Log.d("Iterator", iterator.toString());
while (iterator.hasNext()) {
Entry<String, Integer> entry = iterator.next();
Log.d("Entry", entry.getKey());
arrayList.add(entry.getKey());
}
}
private void getCategory(String cat_name) {
// TODO Auto-generated method stub
if (name.size() > 0)
name.clear();
if (emoticons.size() > 0)
emoticons.clear();
if (arrayList.size() > 0)
arrayList.clear();
Field names[] = R.drawable.class.getFields();
for (Field f : names) {
if (f.getName().contains(cat_name)) {
name.add(f.getName());
}
}
for (int i = 0; i < name.size(); i++) {
int id = getResources().getIdentifier(name.get(i), "drawable",
getPackageName());
Log.d("Drawable Folder", name.get(i).toString());
emoticons.put(name.get(i), id);
}
fillArrayList();
}
private void addListeners() {
// TODO Auto-generated method stub
cat_one.setOnClickListener(this);
gridView.setOnItemClickListener(this);
}
private void addComponents() {
// TODO Auto-generated method stub
editText = (EditText) findViewById(R.id.editText1);
editText.requestFocus();
view = getLayoutInflater().inflate(R.layout.popup, null);
gridView = (GridView) view.findViewById(R.id.gridLayout);
dialog = new Dialog(this, R.style.DialogSlideAnim);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setGravity(Gravity.BOTTOM);
Window window = dialog.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
dialog.setContentView(view);
dialog.setCanceledOnTouchOutside(true);
cat_one = (ImageButton) findViewById(R.id.category_one);
}
public Spannable getSmiledText(Context context, String text) {
SpannableStringBuilder builder = new SpannableStringBuilder(text);
int index;
for (index = 0; index < builder.length(); index++) {
for (Entry<String, Integer> entry : emoticons.entrySet()) {
int length = entry.getKey().length();
if (index + length > builder.length())
continue;
if (builder.subSequence(index, index + length).toString()
.equals(entry.getKey())) {
builder.setSpan(new ImageSpan(context, entry.getValue()),
index, index + length,
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
index += length - 1;
break;
}
}
}
return builder;
}
class ImageAdapter extends BaseAdapter {
public int getCount() {
return arrayList.size();
}
public Object getItem(int position) {
return arrayList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
convertView = LayoutInflater.from(MainActivity.this).inflate(
R.layout.row, null);
ImageView imageView = (ImageView) convertView
.findViewById(R.id.imageView1);
imageView.setBackgroundDrawable(getResources().getDrawable(
emoticons.get(arrayList.get(position))));
Log.d(getLocalClassName(),
String.valueOf(emoticons.get(arrayList.get(position))));
return convertView;
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == cat_one) {
getCategory("good");
gridView.setAdapter(new ImageAdapter());
}
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
// TODO Auto-generated method stub
String value = gridView.getAdapter().getItem(position).toString();
Spannable spannable = getSmiledText(MainActivity.this, value);
SpannableStringBuilder ssb = new SpannableStringBuilder(
editText.getText()).append(spannable).append(" ");
editText.setText(ssb);
editText.setSelection(editText.getText().length());
editText.setCursorVisible(true);
}
}
答案 0 :(得分:3)
我认为我们需要一些追踪才能更好地了解出了什么问题。但是当你无法加载图像时,我想知道你是否失去了对外部存储目录的访问权限
答案 1 :(得分:2)