0
我有一個n應用程序要求用戶從圖庫中選擇圖像,它將以圖像按鈕顯示。我怎樣才能得到一個imagebutton中的圖像的路徑作爲一個字符串?獲取圖像的路徑
請幫助
public class NewPortal extends Activity implements OnClickListener, android.view.View.OnClickListener
{
Bitmap bmp;
public static final int REQUEST_CODE =1;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.portallayout);
ImageButton btn1 = (ImageButton)findViewById(R.id.imageButton2);
btn1.setOnClickListener(this);
Button btn2 = (Button)findViewById(R.id.button2);
btn2.setOnClickListener(this);
}
public void onClick(View v) {
Toast pieceToast=null;
EditText eiteText;
switch (v.getId()) {
case R.id.imageButton2:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(intent, REQUEST_CODE);
int RESULT_LOAD_IMAGE = 1;
break;
case R.id.button2:
eiteText=(EditText)findViewById(R.id.editText2);
String result=eiteText.getText().toString();
MySQLiteEntity entity = new MySQLiteEntity(1,
"Random Post title",
"Image",
path of image ,
result);
MYSQLiteDataSource datasource = new MYSQLiteDataSource(getApplicationContext());
datasource.createPost(entity);
break;
}
}
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult1(requestCode, resultCode, data);
if(requestCode == RESULT_CANCELED) return;
ParcelFileDescriptor fd;
try {
fd = getContentResolver().openFileDescriptor(data.getData(), "r");
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
}
Bitmap bmp = BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor());
ImageButton imgButton = (ImageButton)findViewById(R.id.imageButton2);
imgButton.setImageBitmap(bmp);
}
}