2012-12-11 136 views
4

我想顯示圖像和文本在編輯文本,但它不在冰淇淋三明治和果凍豆。 它在以下版本中正常工作。應用程序不能在Android 4.0及以上版本

當活動被暫停,並再次重新開始時,它會顯示圖像名稱,而不是在編輯文字圖像與文字一起..

請參考圖片和相關的代碼

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); 
} 
    } 

enter image description here

enter image description here

+0

你的問題是沒有任何幫助的具體情況。 –

+0

我應該提供什麼? – moDev

+0

你做了什麼?你檢查過日誌嗎? DDMS輸出?當你再次恢復時,看看DDMS會發生什麼。並在這裏粘貼日誌。 –

回答

3

我認爲我們需要一些追蹤來更好地瞭解發生了什麼事情NG。但是當你不能加載圖像,我在想,如果它可能是你失去了訪問外部存儲目錄

+0

我正在從資源加載圖片(可繪製) – moDev

相關問題