I want to show the image that taken by user gallery to a canvas to edit it after.but till now i can get image from user.but i dont know how to show the image by canvas
this is my code :
public class second extends ActionBarActivity {
private static int RESULT_LOAD_IMAGE = 1;
Bitmap base;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
i know to creat a canvas with Canvas canvas= new Canvas(image) and draw the bitmap by ondraw method.
but still i dont figure out how to convert the photo that given from users gallery to canvas and show it
shouldn't something like this work:
imageView.setImage(yourImage);
imageView.notify();
sry that i had to post thisn as answers, but i dont have enough repfor a simple comment :/
Related
i searched and i found how to can to get image from gallery and show it in image view.this is a my source
public class CredoUploadFile extends Activity {
private EditText credouploadfileuserid, credouploadfileusername,
credouploadfileusersurname;
private int RESULT_LOAD_IMAGE = 1;
private int choosebutton;
private RoundedImageView credouploadcenter1left,credouploadcenter1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_credo_upload_file);
credouploadcenter1 = (RoundedImageView) findViewById(R.id.credouploadcenter1);
credouploadcenter1left = (RoundedImageView) findViewById(R.id.credouploadcenter1left);
credouploadcenter1left.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
choosebutton=1;
}
});
credouploadcenter1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
choosebutton=2;
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
if(choosebutton==1)
credouploadcenter1left.setImageBitmap(BitmapFactory
.decodeFile(picturePath));
if(choosebutton==2)
credouploadcenter1.setImageBitmap(BitmapFactory
.decodeFile(picturePath));
}
}
}
i can to show image from gallery in image view,but i have two imageviews and when i click second imageview and choose some image result is first image view.i can't to show image only imageview witch i clicked.
how i can solve my problem? if anyone knows solution please help me
In onActivityResult you use the same variable credouploadcenter1left for both if...
Try:
if(choosebutton==1)
credouploadcenter1left.setImageBitmap(BitmapFactory.decodeFile(picturePath));
if(choosebutton==2)
credouploadcenter1.setImageBitmap(BitmapFactory .decodeFile(picturePath));
if(choosebutton==1)
credouploadcenter1left.setImageBitmap(BitmapFactory.decodeFile(picturePath));
if(choosebutton==2)
credouploadcenter1left.setImageBitmap(BitmapFactory.decodeFile(picturePath));
Here is the error, independently of your choose, you will set image always on credouploadcenter1left.
You have to set it on centeruploadcenter1 when you choose second button.
How Can I put image from directory instead of load from media. I load from gallery like this
String[] filePathColumn = { MediaStore.Images.Media.DATA };. How can load from /assets or /drawable-mdpi ? This is my code. Kindly tell me how can i used this code to add image from my directory?
public class ImageGalleryDemoActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
You can put the respective drawables in the android drawables folders and Android would automatically choose the required drawable based upon the device display density. You can set the drawables as
imageView = (ImageView)findViewById(R.id.imageView);
imageView.setImageDrawable(getResources().getDrawable(R.drawable.image));
You can also take help from here.
I have an xml layout in which there's a button and a text field. I fill in some message in the text field. Now when i click on the button i need to access the gallery and select an image. How to do get the message encrypted into this image and save it to my gallery again? Also what all types of encryption can be done on this image like AES
The function Selimg() is called by a button and when i click it, it should open the gallery for me to select an image. That image should be stored in 'thumbnail'. So that i can further encrypt it. This is my code so far :
public class EncryptActivity extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
private static final int PICK_FROM_GALLERY = 2;
Bitmap thumbnail = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.act_encrypt);
}
public void Selimg(){
Intent in = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(in, RESULT_LOAD_IMAGE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data){
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
thumbnail = (BitmapFactory.decodeFile(picturePath));
}
}
i am using a button to select an image from gallery and the image will be set to imageview which is on other screen:
//setimg button is on firstscreen.xml
setimg.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
// String picturePath contains the path of selected Image
ImageView iv_wallset = (ImageView) findViewById(R.id.iv_wallset);
iv_wallset.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
// Imageview iv_wallset is on second.xml
can we use intent.putextra() to carry image from one screen to other???
Yes, you can put an extra String in the intent and from the second activity get the argument and decode the picture:
in activity 1:
mIntent.putExtra("image_path", picturePath);
in activity 2:
String path = getIntent().getStringExtra("image_path");
ImageView imageView = (ImageView) findViewById(R.id.iv_wallset);
imageView.setImageBitmap(BitmapFactory.decodeFile(path));
yes convert image into byte array and pass that byte array to other activity.
this is the code i'm using currently,the code only displays the image from gallery. but i want to access the pixels of the image.i am confused exactly where the image data is stored in the code as in which variable.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
Please refer this Answer on SO, here is described to get Pixels, you should use:
Bitmap bm = BitmapFactory.decodeFile(picturePath);
and then :
bm.getPixel(x,y);// will return an int that corresponds to an int in the Color class, such as Color.BLACK or Color.WHITE.
Hope this may help you