How to access an image from the phone's photo gallery?
You have to launch the Gallery App using the built-in Intents
. After that, on your onActivityResult()
, get the path of the selected image and load your image into your ImageView
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/loadimage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Load Image"
/>
<TextView
android:id="@+id/targeturi"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/targetimage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
Your Activity
package com.exercise.AndroidSelectImage;
import java.io.FileNotFoundException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class AndroidSelectImage extends Activity {
TextView textTargetUri;
ImageView targetImage;
/** 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.loadimage);
textTargetUri = (TextView)findViewById(R.id.targeturi);
targetImage = (ImageView)findViewById(R.id.targetimage);
buttonLoadImage.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 0);
}});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
Uri targetUri = data.getData();
textTargetUri.setText(targetUri.toString());
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
targetImage.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
How to get images in gallery on android phone?
you can get images by initiating this intent:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
startActivityForResult(Intent.createChooser(intent, "Select File"), SELECT_FILE);
then handle it in onActivityResult()
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_FILE)
onSelectFromGalleryResult(data);
}
}
private void onSelectFromGalleryResult(Intent data) {
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
let me know if it help you
how get images from gallery in my phone and show in page with flutter?
I use phoro_manager library and show image and video from gallery like image that shown in question.
If you have a better solution, I would be happy to know it
how can i get image from my phone gallery into my application
Please check if b=findViewById(R.id.button);
belongs in activity_main.xml.
null object references result when your findViewById references an id value that is not a part of the XML defined in setContentView()
Get any image from android phone gallery and use in a ImageView
Ok.
Finally I solved my question.
First I create a cursor and get all the images I have in my gallery phone and get the last photo I took.
Cursor cursor = pickLastPhotoAlbum();
After I move the cursor to first line, get the quantity of lines the cursor returned and create a Drawable.
cursor.moveToFirst();
int qtd = cursor.getCount();
Drawable backgroundGaleria;
Third I made an if to see if my cursor is null or not.
If is null I get a drawable image that I have in my imgs folder.
If is not null I get the path of the image, set my drawable from path and set the background of my ImageView with this drawable.
if (qtd > 0){
String imageGallery = Environment.getExternalStorageDirectory()+ "/Tubagram/" + cursor.getString(1)+".png";
backgroundGaleria = Drawable.createFromPath(imageGallery);
principalActivity_iv_UltimaFoto.setBackground(backgroundGaleria);
}else{
principalActivity_iv_UltimaFoto.setBackgroundResource(R.drawable.box_imagem_album);
}
To the image doesn't overflow his size you need to set the width and height (I set min, normal size and max) using XXXdp.
android:layout_width="61dp"
android:layout_height="42dp"
And here is the method who returns values to my cursor
private Cursor pickLastPhotoAlbum(){
final ContentResolver cr = getContentResolver();
final String[] p1 = new String[] {
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.TITLE,
MediaStore.Images.ImageColumns.DATE_TAKEN
};
Cursor c1 = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, p1, null, null, p1[1] + " DESC");
if (c1.moveToFirst() ) {
Log.i("Teste", "last picture (" + c1.getString(1) + ") taken on: " + new Date(c1.getLong(2)));
}
Log.i("Caminho download imagem", "file://"+Environment.getExternalStorageDirectory()+ "/Tubagram/" + c1.getString(1) + ".png");
return c1;
}
Thanks to everyone who helped me with this.
Android Studio - How do you get image from gallery and then save it
U could save the picturePath string into a file. A really easy way to do this is by using the Properties object.
Then each time when you open the activity, you open the properties file and read out the path that is saved. Then you can set it again with a part of the code that you allready have.
Android get image from gallery into ImageView
Run the app in debug mode and set a breakpoint on if (requestCode == SELECT_PICTURE)
and inspect each variable as you step through to ensure it is being set as expected. If you are getting a NPE on img.setImageURI(selectedImageUri);
then either img
or selectedImageUri
are not set.
Related Topics
Android Cardview Remove Padding
Push Up Content When Clicking in Edit Text
How to Use Data Binding and Kotlin in Android Studio 3.0.0
Status Bar Turns White and Does Not Show Content Behind It
Rendering Problems in Android Studio V 1.1/1.2
Cannot Deserialize Instance of Object Out of Start_Array Token in Spring Webservice
How to Rotate Textview 90 Degrees and Display
Webview Rendering Issue in Android Kitkat
Capturing Images with Mediastore.Action_Image_Capture Intent in Android
How to Include a Library Module Dependency in an Android Studio Project
Event for Videoview Playback State or Mediacontroller Play/Pause
Update Support Library 23.2.0 Build Bug
Lint Error "Do Not Treat Position as Fixed; Only Use Immediately..."
How to Detect a Dual-Core CPU on an Android Device from Code
How to Change Actionmode Background Color in Android