TextView 和EditText显示图片方法

打开本地图片
Intent picture = new Intent(Intent.ACTION_GET_CONTENT);
picture.setType("image/*"); 				picture.addCategory(Intent.CATEGORY_OPENABLE);			    startActivityForResult(Intent.createChooser(picture, "选择图片"), 0);


的一种方式  

private ImageGetter imageGetter = new ImageGetter(){    
	        @Override    
	        public Drawable getDrawable(String source)    
	        {    
	            int id = Integer.parseInt(source);    
	            Drawable d = getResources().getDrawable(id);    
	            d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());    
	            return d;    
	        }    
		};


调用方式  
 edit.append(Html.fromHtml("<img src='"+R.drawable.ic_launcher+"'/>", imageGetter, null));



第二种方式:



public void srcrem(Bitmap bmps){
	

	Drawable drawable = new BitmapDrawable(bmps);
	//Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);    
	drawable.setBounds(0, 0,  
	         drawable.getIntrinsicWidth(),   
	         drawable.getIntrinsicHeight());    
	   //需要处理的文本,[smile]是需要被替代的文本  
	String str = "0";
	   SpannableString spannable = new SpannableString(str);    
	   //要让图片替代指定的文字就要用ImageSpan    
	   ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BASELINE);    
	   //开始替换,注意第2和第3个参数表示从哪里开始替换到哪里替换结束(start和end)    
	   //最后一个参数类似数学中的集合,[5,12)表示从5到12,包括5但不包括12    
	   spannable.setSpan(span, 0,0, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);      
	   edit.setText(spannable);   
	
}






@Override
		protected void onActivityResult(int requestCode, int resultCode,Intent data) {
			super.onActivityResult(requestCode, resultCode, data);  
			 if(resultCode == RESULT_OK){  
			        Uri uri = data.getData();   
			        String [] proj={MediaStore.Images.Media.DATA};  
			        Cursor cursor = managedQuery( uri,  
			                proj,                 // Which columns to return  
			                null,       // WHERE clause; which rows to return (all rows)  
			                null,       // WHERE clause selection arguments (none)  
			                null);                 // Order-by clause (ascending by name)  
			          
			        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);  
			        cursor.moveToFirst();  
			          
			        String path = cursor.getString(column_index);  
			        bmp = BitmapFactory.decodeFile(path);  
			        System.out.println("the path is :" + path);  
			     
				        srcrem(bmp);
				       // <img src=\""/sdcard/images/test.jpg"\"/>   
			      // edit.append(Html.fromHtml("<img src='"+R.drawable.ic_launcher+"'/>", imageGetter, null));
				   //edit.append(Html.fromHtml(sText2, imageGetter, null)); 
				  
			    }else{  
			        Toast.makeText(Main.this, "请重新选择图片", Toast.LENGTH_SHORT).show();  
			    }  
			 
			
		} 





转换Bitmap to Drawable
Bitmap bitmap = new Bitmap (...);       
Drawable drawable = new BitmapDrawable(bitmap);


转换Drawable to Bitmap
Drawable d = ImagesList.get(0);    
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();

你可能感兴趣的:(TextView 和EditText显示图片方法)