I have a code ,in which i am able to select any image from gallery and attached it into my activity but i want same thing for any mp3 files or any text files as in attachement.
Here is my code:
// b1 is my button.
b1.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.EXTERNAL_CONTENT_URI);
startActivityForResult(i,SELECT_PHOTO);
}
});
// And returning result from Gallery:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_PHOTO && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
System.out.println("Path="+selectedImage);
String[] filePathColumn = { MediaStore.Images.Media.DATA,MediaStore.Audio.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();
Bitmap bitmap=BitmapFactory.decodeFile(picturePath);
img.setImageBitmap(bitmap);
}
}
You can do for MP3
Intent pickMedia = new Intent(Intent.ACTION_GET_CONTENT);
pickMedia.setType("audio/*");
startActivityForResult(pickMedia,1);
Use this code:
b1.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);
}
});
// And returning result from Gallery:
if (requestCode == RESULT_LOAD_IMAGE && 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.register_photo);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
Related
I am trying to add Image from Gallery to show up in my app. But it is not showing after I select the image from the gallery. It is not showing any error and runs successfully on my device.
private static int RESULT_LOAD_IMAGE=1;
public void addImage(View view){
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 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.memeImage);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
try this with uri
imageView.setImageUri(selectedImage )
This question already has answers here:
android pick images from gallery
(19 answers)
Closed 5 years ago.
Source Code for get image from gallery and set it to image view and i want to pass the selected image in another activity and set it to linear layout.
private void galleryIntent() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
}
private void onSelectFromGalleryResult(Intent data) {
bitmap = null;
if (data != null) {
filePath = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
System.out.println("bitmap is :" +bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
set.setImageBitmap(bitmap);
}
private static int RESULT_LOAD_IMAGE = 1;
#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();
Intent i=new Intent(this,NextActivity.class);
i.putExtra("path",picturePath);
startActivity(i);
}
}
NextActivity File :-
String picturePath =getIntent().getStringExtra("path");
LinearLayout imageView = (LinearLayout) findViewById(R.id.imgView);
Bitmap bmImg = BitmapFactory.decodeFile(picturePath);
BitmapDrawable background = new BitmapDrawable(bmImg);
imageView.setBackgroundDrawable(background);
Used from https://stackoverflow.com/a/26403116/8603832
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2 && 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();
bitmap = BitmapFactory.decodeFile(picturePath);
image.setImageBitmap(bitmap);
if (bitmap != null) {
ImageView rotate = (ImageView) findViewById(R.id.rotate);
}
}
pass selectedImage URI in Other Activity and using intent and use same code for generate the Bitmap with that URI
I have three imageViews on my Activity. In each ImageView I want to upload another picture. If the user click on the icon (image1/image2/image3) the galery opens so the user can choose a pic to upload it on the imageView. At my code, i have only one imageView works.
So my question. How can i modify the onActivityResult Method to check which imageView is clickt, and call the setImageBitmapMethod on it?
Here is my code
image1 = (ImageView)findViewById(R.id.image_three_1);
image2 = (ImageView)findViewById(R.id.image_three_2);
image3 = (ImageView)findViewById(R.id.image_three_3);
image1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
});
image2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
});
image3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 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();
image1.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
Thanks for the tip payal tuteja. I made three different Result Variables (RESULT_LOAD_IMAGE1 / RESULT_LOAD_IMAGE2 / RESULT_LOAD_IMAGE3 ) and then i set the right imageView on the right RESULT_LOAD_IMAGE1 Variable.
Here the code that works for me
private static final int RESULT_LOAD_IMAGE1 = 1;
private static final int RESULT_LOAD_IMAGE2 = 2;
private static final int RESULT_LOAD_IMAGE3 = 3;
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout_for_three);
//load titles from string.xml
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
//load icons from string.xml
navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
set(navMenuTitles, navMenuIcons);
image1 = (ImageView)findViewById(R.id.image_three_1);
image2 = (ImageView)findViewById(R.id.image_three_2);
image3 = (ImageView)findViewById(R.id.image_three_3);
image1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE1);
}
});
image2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE2);
}
});
image3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE3);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE1 && 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();
image1.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
if (requestCode == RESULT_LOAD_IMAGE2 && 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();
image2.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
if (requestCode == RESULT_LOAD_IMAGE3 && 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();
image3.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
I have Written code for getting the Image from gallery and set into the Image View but the image is not set.This is my problem suggest me guys
Here is My Code:
btnUpload.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
getActivity().startActivityForResult(i, 1);
}
});
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && null != data)
{
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
//int pic=Integer.parseInt(picturePath);
cursor.close();
//ImageView imageView = (ImageView) mFormView.findViewById(R.id.imageView);
ivMan.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
thanks in Advance
Replace getActivity().startActivityForResult(i, 1); with
startActivityForResult(i, 1);
for getting image from gallery
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
Check if you have a proper path if yes than decode the image and set to imageview.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_IMAGE && data != null && data.getData() != null) {
Uri _uri = data.getData();
//User had pick an image.
Cursor cursor = getContentResolver().query(_uri, new String[]{android.provider.MediaStore.Images.ImageColumns.DATA}, null, null, null);
cursor.moveToFirst();
//Link to the image
final String imageFilePath = cursor.getString(0);
cursor.close();
File file = new File(imageFilePath);
if (file.exists()) {
Bitmap bMap = decodeFile(file);
// asset.setImageBitmap(bMap);
asset.setImageBitmap(bMap);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
this will defiantly help you
I am making a project in which in which i am using two ImageViews and i want to pick two different images from gallery and i want to set on two different ImageViews. I am able to pick one and set on one imageview. but i am not coming to understand that how i can do this in second imageview case?
The code i used is...
ImageView iv1,iv2;
private static int RESULT_LOAD_IMAGE = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
iv1=(ImageView)findViewById(R.id.imageView21);
iv1.setOnClickListener(this);
iv2=(ImageView)findViewById(R.id.imageView21);
iv2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.imageView21){
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) {
// TODO Auto-generated method stub
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();
iv1.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
Update 1...
It is giving problem, it is settingup image on both imageview even if i am trying to do for one...
public class LayoutDisplay2 extends Activity{
ImageView iv1, iv2;
private static int RESULT_LOAD_IMAGE1 = 1;
private static int RESULT_LOAD_IMAGE2 = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
iv1 = (ImageView) findViewById(R.id.imageView21);
iv1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE1);
}
});
iv2 = (ImageView) findViewById(R.id.imageView22);
iv2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent in = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(in, RESULT_LOAD_IMAGE2);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE1 && 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();
iv1.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
if (requestCode == RESULT_LOAD_IMAGE2 && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Toast.makeText(getApplicationContext(), "in second",
Toast.LENGTH_SHORT).show();
Log.i("Second", "in second");
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
iv2.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
Try like below:
ImageView iv1, iv2;
private static int RESULT_LOAD_IMAGE1 = 1;
private static int RESULT_LOAD_IMAGE2 = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
iv1 = (ImageView) findViewById(R.id.imageView21);
iv1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE1);
}
});
iv2 = (ImageView) findViewById(R.id.imageView22);
iv2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent in = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(in, RESULT_LOAD_IMAGE2);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE1 && 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();
iv1.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
if (requestCode == RESULT_LOAD_IMAGE2 && resultCode == RESULT_OK
&& null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Toast.makeText(getApplicationContext(), "in second",
Toast.LENGTH_SHORT).show();
Log.i("Second", "in second");
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
iv2.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
There are some mistakes in your code.
You have given same id for both of the imageviews.
iv1=(ImageView)findViewById(R.id.imageView21);
iv2=(ImageView)findViewById(R.id.imageView21);
Change this to:
iv1=(ImageView)findViewById(R.id.imageView21);
iv2=(ImageView)findViewById(R.id.imageView22);
Make changes as below
if(v.getId()==R.id.imageView21){
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE_1);
}
else if(v.getId()==R.id.imageView22){
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE_2);
}
Try this image-chooser-library-1.2.9.jar
Is simple and easy
imageChooserManager = new ImageChooserManager(this, ChooserType.REQUEST_PICK_PICTURE);
imageChooserManager.setImageChooserListener(this);
imageChooserManager.choose();
Follow on this link you may get answer.
https://github.com/coomar2841/image-chooser-library
http://techdroid.kbeanie.com/2013/03/easy-image-chooser-library-for-android.html