任何人都可以帮我解决这个问题,如何将图像从gallery / imagebutton调用到画布进行绘图? 这是我的代码段:
buah1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getBaseContext(), BelajarMewarnai.class);
startActivity(intent);
}
});
如果有人需要一个项目,我会将我的项目发送到电子邮件。
答案 0 :(得分:2)
其实我想在你的评论中发布这个链接,但你似乎对android不太熟悉(我这么认为),所以我发布我的代码来做到这一点。首先选择哪个按钮点击你想让用户被引导到图库(只有默认图库而不是其他任何东西,如果你从任何其他图片中选择图片,你可能会得到一个空指针)。
点击该按钮,执行以下操作:
private void onClickOfButton(View v){
Intent galleryIntent=new Intent();
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(Intent.createChooser(galleryIntent, "pic-select"), 10);//(10 its just a request code, u can give ur own, but same should there at receiving end )
}
因为你已经开始了一个结果活动,所以你应该等待来自该开始活动的结果,这样活动就会带来你的图像和你的活动你只是收集它并把它放到像这样的图像视图中:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==10 && resultCode==Activity.RESULT_OK){
try{
Uri selectImageUri=data.getData();
String selectedImagePath=getPath(selectImageUri);
Bitmap pic=BitmapFactory.decodeFile(selectedImagePath);
if(pic!=null){
yourImageView.setImageBitmap(pic);
}
}catch(NullPointerException ex){
Toast.makeText(getActivity().getBaseContext(), "Go to default gallery area and choose a pic", Toast.LENGTH_LONG).show();
}
}
else
super.onActivityResult(requestCode, resultCode, data);
}
getPath方法:
private String getPath(Uri uri) {
if( uri == null ) {
return null;
}
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().managedQuery(uri, projection, null, null, null);
if( cursor != null ){
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
return uri.getPath();
}
所以这将是你的工作......
答案 1 :(得分:0)
试试这个:
buah1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(getBaseContext(), BelajarMewarnai.class);
Bundle bundle = new Bundle();
//Get the image name you clicked on
String imageName = getResources().getResourceName(imageId);;
//Send the image name as a string, which you want to load in the other activity
bundle.putString("image_to_load", imageName.split("/")[1].toString());
intent.putExtras(bundle);
startActivity(intent);
}
});
然后您可以在onCreate的下一个活动中获取您的图像名称,如下所示:
//Here you can get the image name you already clicked in the previous activity to load in your canvas
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
String image_name = bundle.getString("image_to_load")
}
然后您可以将图像名称设置为imageView,如下所示:
//Here you can get all resources in res folder
Resources res = getResources();
//define a new string which takes the image name you get from extras in the above code
String mDrawableName = image_name;
//Now you have the image name you clicked in theprevious activity, and ResID is to get the resource id from its name
int resID = res.getIdentifier(mDrawableName, "drawable", getPackageName());
//Here you can get the drawable from the resourceID you obtained in the above line
Drawable drawable = res.getDrawable(resID );
YOUR_IMAGE is the ImageView of the canvas you want to load the image inside in order to draw or whatever you want to do
drawView.setBackgroundDrawable(drawable);
我希望这会有所帮助..