今天遇到了一个头疼的问题,就是在popupwindow或者dialog中有一个edittext 我用了onActivityResult通过图库现则照片显示在edittext,但是选择后没有图片 研究了下 他根本就没有钓友onActivityResult方法 大家有没有遇到过这种情况,帮帮忙!代码如下:
public void CreatNoteContent()
{
widgetnum[0]=1;
widgetnum[1]=0;
widgetnum[2]=0;
//LinearLayout creatnotelayout=new LinearLayout(NoteListActivity.this);
//noteword=new EditText[widgetnum[0]];
LayoutInflater creattitleflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View notecreatcontentlayout=creattitleflater.inflate(R.layout.notecreatcontentlayout, null);
notecreatcontentlayout.setBackgroundResource(R.drawable.bg_paper);
ImageButton voicebutton=(ImageButton)notecreatcontentlayout.findViewById(R.id.notecontentvoicebutton);
ImageButton drawbutton=(ImageButton)notecreatcontentlayout.findViewById(R.id.notecontentdrawbutton);
ImageButton creatcontentbutton=(ImageButton)notecreatcontentlayout.findViewById(R.id.notecontentokbutton);
//事件响应
creatcontentbutton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
creatnotecontentdialog.dismiss();
}
});
drawbutton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
//showDialog(1);
Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
getImage.addCategory(Intent.CATEGORY_OPENABLE);
getImage.setType("image/*");
startActivityForResult(getImage, PHOTOCURSOR);
}
});
creatnotecontentdialog=new Dialog(NoteListActivity.this);
creatnotecontentdialog.setContentView(notecreatcontentlayout);
creatnotecontentdialog.show();
//显示
/*creatnotecontentpop=new PopupWindow(notecreatcontentlayout,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
creatnotecontentpop.setOutsideTouchable(false);
creatnotecontentpop.setAnimationStyle(R.style.PopupAnimation);
creatnotecontentpop.update();
creatnotecontentpop.setTouchable(true);
creatnotecontentpop.setFocusable(true);
creatnotecontentpop.showAtLocation(findViewById(R.id.noteshowandcreatlayout), Gravity.TOP, 0, 0);
*/
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
ContentResolver resolver = getContentResolver();
Log.d("图片", "0000");
if (resultCode == RESULT_OK) {
switch (requestCode) {
case PHOTOCURSOR:
Log.d("图片", "1111111");
//获得图片的uri
Uri originalUri = intent.getData();
Bitmap bitmap = null;
try {
Log.d("图片", "222222");
Bitmap originalBitmap = BitmapFactory.decodeStream(resolver.openInputStream(originalUri));
bitmap = resizeImage(originalBitmap, 200, 200);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(bitmap != null){
Log.d("图片", "333333");
//根据Bitmap对象创建ImageSpan对象
ImageSpan imageSpan = new ImageSpan(NoteListActivity.this, bitmap);
//创建一个SpannableString对象,以便插入用ImageSpan对象封装的图像
SpannableString spannableString = new SpannableString("[local]"+1+"[/local]");
// 用ImageSpan对象替换face
spannableString.setSpan(imageSpan, 0, "[local]1[local]".length()+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//将选择的图片追加到EditText中光标所在位置
int index = contentdite1.getSelectionStart(); //获取光标所在位置
Editable edit_text = contentdite1.getEditableText();
contentdite1.setText("ssss");
if(index <0 || index >= edit_text.length()){
edit_text.append(spannableString);
}else{
edit_text.insert(index, spannableString);
}
}else{
Toast.makeText(NoteListActivity.this, "获取图片失败", Toast.LENGTH_SHORT).show();
}
break;
case CAMERACURSOR:
Log.d("图片", "444444");
Bundle extras = intent.getExtras();
Bitmap originalBitmap1 = (Bitmap) extras.get("data");
if(originalBitmap1 != null){
Log.d("图片", "555555");
bitmap = resizeImage(originalBitmap1, 200, 200);
//根据Bitmap对象创建ImageSpan对象
ImageSpan imageSpan = new ImageSpan(NoteListActivity.this, bitmap);
//创建一个SpannableString对象,以便插入用ImageSpan对象封装的图像
SpannableString spannableString = new SpannableString("[local]"+1+"[/local]");
// 用ImageSpan对象替换face
spannableString.setSpan(imageSpan, 0, "[local]1[local]".length()+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//将选择的图片追加到EditText中光标所在位置
int index = contentdite1.getSelectionStart(); //获取光标所在位置
Editable edit_text = contentdite1.getEditableText();
if(index <0 || index >= edit_text.length()){
edit_text.append(spannableString);
}else{
edit_text.insert(index, spannableString);
}
}else{
Toast.makeText(NoteListActivity.this, "获取图片失败", Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
}
/**
* 图片缩放
* @param originalBitmap 原始的Bitmap
* @param newWidth 自定义宽度
* @param newHeight自定义高度
* @return 缩放后的Bitmap
*/
private Bitmap resizeImage(Bitmap originalBitmap, int newWidth, int newHeight){
int width = originalBitmap.getWidth();
int height = originalBitmap.getHeight();
//定义欲转换成的宽、高
// int newWidth = 200;
// int newHeight = 200;
//计算宽、高缩放率
float scanleWidth = (float)newWidth/width;
float scanleHeight = (float)newHeight/height;
//创建操作图片用的matrix对象 Matrix
Matrix matrix = new Matrix();
// 缩放图片动作
matrix.postScale(scanleWidth,scanleHeight);
//旋转图片 动作
//matrix.postRotate(45);
// 创建新的图片Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(originalBitmap,0,0,width,height,matrix,true);
return resizedBitmap;
}
public void CreatNoteContent()
{
widgetnum[0]=1;
widgetnum[1]=0;
widgetnum[2]=0;
//LinearLayout creatnotelayout=new LinearLayout(NoteListActivity.this);
//noteword=new EditText[widgetnum[0]];
LayoutInflater creattitleflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View notecreatcontentlayout=creattitleflater.inflate(R.layout.notecreatcontentlayout, null);
notecreatcontentlayout.setBackgroundResource(R.drawable.bg_paper);
ImageButton voicebutton=(ImageButton)notecreatcontentlayout.findViewById(R.id.notecontentvoicebutton);
ImageButton drawbutton=(ImageButton)notecreatcontentlayout.findViewById(R.id.notecontentdrawbutton);
ImageButton creatcontentbutton=(ImageButton)notecreatcontentlayout.findViewById(R.id.notecontentokbutton);
//事件响应
creatcontentbutton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
creatnotecontentdialog.dismiss();
}
});
drawbutton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
//showDialog(1);
Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
getImage.addCategory(Intent.CATEGORY_OPENABLE);
getImage.setType("image/*");
startActivityForResult(getImage, PHOTOCURSOR);
}
});
creatnotecontentdialog=new Dialog(NoteListActivity.this);
creatnotecontentdialog.setContentView(notecreatcontentlayout);
creatnotecontentdialog.show();
//显示
/*creatnotecontentpop=new PopupWindow(notecreatcontentlayout,LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
creatnotecontentpop.setOutsideTouchable(false);
creatnotecontentpop.setAnimationStyle(R.style.PopupAnimation);
creatnotecontentpop.update();
creatnotecontentpop.setTouchable(true);
creatnotecontentpop.setFocusable(true);
creatnotecontentpop.showAtLocation(findViewById(R.id.noteshowandcreatlayout), Gravity.TOP, 0, 0);
*/
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
ContentResolver resolver = getContentResolver();
Log.d("图片", "0000");
if (resultCode == RESULT_OK) {
switch (requestCode) {
case PHOTOCURSOR:
Log.d("图片", "1111111");
//获得图片的uri
Uri originalUri = intent.getData();
Bitmap bitmap = null;
try {
Log.d("图片", "222222");
Bitmap originalBitmap = BitmapFactory.decodeStream(resolver.openInputStream(originalUri));
bitmap = resizeImage(originalBitmap, 200, 200);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(bitmap != null){
Log.d("图片", "333333");
//根据Bitmap对象创建ImageSpan对象
ImageSpan imageSpan = new ImageSpan(NoteListActivity.this, bitmap);
//创建一个SpannableString对象,以便插入用ImageSpan对象封装的图像
SpannableString spannableString = new SpannableString("[local]"+1+"[/local]");
// 用ImageSpan对象替换face
spannableString.setSpan(imageSpan, 0, "[local]1[local]".length()+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//将选择的图片追加到EditText中光标所在位置
int index = contentdite1.getSelectionStart(); //获取光标所在位置
Editable edit_text = contentdite1.getEditableText();
contentdite1.setText("ssss");
if(index <0 || index >= edit_text.length()){
edit_text.append(spannableString);
}else{
edit_text.insert(index, spannableString);
}
}else{
Toast.makeText(NoteListActivity.this, "获取图片失败", Toast.LENGTH_SHORT).show();
}
break;
case CAMERACURSOR:
Log.d("图片", "444444");
Bundle extras = intent.getExtras();
Bitmap originalBitmap1 = (Bitmap) extras.get("data");
if(originalBitmap1 != null){
Log.d("图片", "555555");
bitmap = resizeImage(originalBitmap1, 200, 200);
//根据Bitmap对象创建ImageSpan对象
ImageSpan imageSpan = new ImageSpan(NoteListActivity.this, bitmap);
//创建一个SpannableString对象,以便插入用ImageSpan对象封装的图像
SpannableString spannableString = new SpannableString("[local]"+1+"[/local]");
// 用ImageSpan对象替换face
spannableString.setSpan(imageSpan, 0, "[local]1[local]".length()+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//将选择的图片追加到EditText中光标所在位置
int index = contentdite1.getSelectionStart(); //获取光标所在位置
Editable edit_text = contentdite1.getEditableText();
if(index <0 || index >= edit_text.length()){
edit_text.append(spannableString);
}else{
edit_text.insert(index, spannableString);
}
}else{
Toast.makeText(NoteListActivity.this, "获取图片失败", Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}
}
/**
* 图片缩放
* @param originalBitmap 原始的Bitmap
* @param newWidth 自定义宽度
* @param newHeight自定义高度
* @return 缩放后的Bitmap
*/
private Bitmap resizeImage(Bitmap originalBitmap, int newWidth, int newHeight){
int width = originalBitmap.getWidth();
int height = originalBitmap.getHeight();
//定义欲转换成的宽、高
// int newWidth = 200;
// int newHeight = 200;
//计算宽、高缩放率
float scanleWidth = (float)newWidth/width;
float scanleHeight = (float)newHeight/height;
//创建操作图片用的matrix对象 Matrix
Matrix matrix = new Matrix();
// 缩放图片动作
matrix.postScale(scanleWidth,scanleHeight);
//旋转图片 动作
//matrix.postRotate(45);
// 创建新的图片Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(originalBitmap,0,0,width,height,matrix,true);
return resizedBitmap;
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货