I want to take a picture from my gallery and i want to give it an "id" to use my android application.
Asked
Active
Viewed 129 times
1
-
Is this application supposed to run on any phone besides yours? – Andrew Thompson Jul 23 '12 at 09:05
-
2there are plenty of posts similar to this... first search – Manikandan Jul 23 '12 at 09:07
-
possible duplicate of [How to capture an image and store it with the native Android Camera](http://stackoverflow.com/questions/3442462/how-to-capture-an-image-and-store-it-with-the-native-android-camera) – Keppil Jul 23 '12 at 09:13
2 Answers
1
On button click even, call the following code....
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, GET_IMAGE);
and, on activity result, write following code:
if (requestCode == GET_IMAGE) {
targetUri = data.getData();
imageView.setImageURI(targetUri);
}
where, private static final int GET_IMAGE = 2;

yatskevich
- 2,085
- 16
- 25

Rushi
- 230
- 1
- 11
0
You can try below code
btnChoosePhoto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(i, 1);
}
});
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && requestCode == 1 ) {
final Uri selectedImage = data.getData();
try {
bitmap = Media.getBitmap(getContentResolver(),selectedImage);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
//path = selectedImage.getEncodedPath();
// create new file to write the encrypted bytes of image in file
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator
+ filename);
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
imageView.setImageBitmap(bitmap);
}
}

Nirali
- 13,571
- 6
- 40
- 53
-
thank you for your answer. But i won't use this picture as an imageView. I wanna change an other class' background with this image. can i do ? – Adem Seferi Jul 23 '12 at 12:00