Image browse button in android activity - android

currently I'm developing an application which requires a browse button in my activity. When I press the browse button I should be able to browse the image files in my sdcard as well as in the phone memory. Upon tapping the image file it should be selected and the path of the image file (that is, where that selected image file is located) should be displayed in the activity in a textview. Also the selected image should be displayed in a imageview. How to do this? Can someone please help me out..

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">
<ImageView
android:id="#+id/imgView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"></ImageView>
<Button
android:id="#+id/buttonLoadPicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="Load Picture"
android:layout_gravity="center"></Button>
</LinearLayout>
MainActivity.java
package com.example.yourpackage;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
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();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
Add Permission in manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

MediaStore.Images.Media.DATA is deprecated. So #Looking_Forward bro's code will not work on android 10+. But just with a simple modification, I worked #Looking_Forward bro's code. Process below:
After Uri selectedImage = data.getData(); line remove all code and just use Glide.with(this).load(selectedImage).into(imageView) (this is Glide library). Also it's much easier then use contentProvider (which is #Looking_Forward bro showed).

Related

Save image to the internal storage

I wanted to to ask how could i store an image, that will take from gallery and store it to a specific folder.
the location i want to store is Shortcut/Images
i'm posting my code and i want solution after this how to store this image into the location above (The location is not yet created)..
Please keep in mind i want to store image in internal storage so that my app could run in hybrid phone as well
package com.example.mohitgupta.shortcut;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
public class GetImage extends AppCompatActivity {
EditText imageName;
private ImageButton img;
private String name;
private Uri selectedImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_get_image);
imageName = (EditText) findViewById(R.id.ImageName);
name = imageName.getText().toString().trim();
}
public void GetImageIntent(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, 1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK) {
selectedImage = data.getData();
img = (ImageButton) findViewById(R.id.imageSelected);
img.setImageURI(selectedImage);
}
}
public void SaveButtonClicked(View view){
BitmapDrawable drawable = (BitmapDrawable) img.getDrawable();
Bitmap bitmap = drawable.getBitmap();
//Toast.makeText(GetImage.this,"Image Saved",Toast.LENGTH_SHORT).show();
}
}
layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.mohitgupta.shortcut.GetImage">
<ImageButton
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_margin="15dp"
android:layout_gravity="center"
android:id="#+id/imageSelected"
android:onClick="GetImageIntent"
android:scaleType="fitCenter"
android:backgroundTint="#000000"
android:src="#drawable/ic_add_a_photo_white_24dp"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_margin="15dp"
android:hint="Enter Name of Image"
android:id="#+id/ImageName"
android:padding="10dp"
android:background="#drawable/shapes"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="Save"
android:onClick="SaveButtonClicked"/>
</LinearLayout>

Android Studio: How to replace image button with photo after taking a photo in android camera

Could anybody help me with a problem that I have?
I would like to make an activity in which i have a make photo button (imageButton_add_playground_image) and after pressing it my app opens up camera for me to take a photo (just one photo).
After making a photo I would like to send it to this activity and change make photo button to a photo made by user (but with leaving make photo button activity on it so the photo could be replaced with different one if this one isn't good enough).
My code in *.java file is as follows:
<!-- begin snippet -->
import android.app.Activity;
import android.content.Intent;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RatingBar;
import android.widget.Toast;
import static ???.MainActivity.REQUEST_IMAGE_CAPTURE;
public class AddPlayground extends AppCompatActivity {
EditText playGroundName;
RatingBar rate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = getIntent();
setContentView(R.layout.activity_add_playground);
playGroundName = (EditText) findViewById(R.id.editText_playground_name);
rate = (RatingBar) findViewById(R.id.ratingBar);
Double message = intent.getDoubleExtra(MainActivity.EXTRA_MESSAGE, 0);
Toast.makeText(this, String.valueOf(message), Toast.LENGTH_SHORT).show();
}
public void addPhoto(View view) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
}
<!-- end snippet -->
And my code in *.xml file is as follows:
<!-- begin snippet -->
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="???.AddPlayground">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="368dp"
android:layout_height="495dp"
android:weightSum="1"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">
<EditText
android:id="#+id/editText_playground_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="Name of playground" />
<ImageButton
android:id="#+id/imageButton_add_playground_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.52"
android:onClick="addPhoto"
app:srcCompat="#drawable/ic_menu_camera" />
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:visibility="visible" />
<EditText
android:id="#+id/editText_playground_comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textMultiLine"
android:hint="Playground description" />
<Button
android:id="#+id/button_add_playground"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="addNewPlayground"
android:text="Add" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
<!-- end snippet -->
Under the ??? there is something else but I removed it as it is not crucial to this question.
when you are starting with tutorials you should read whole text... HERE you have full doc (your method addPhoto is 1:1 copy of dispatchTakePictureIntent method in there)
in short you also need to handle returned bitmap (thumbnail)
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
yourImageView.setImageBitmap(imageBitmap);
}
}
receiving full sized Bitmap is described in Save the Full-size Photo section of above linked doc
The Android Camera application encodes the photo in the return Intent delivered to onActivityResult() as a small Bitmap in the extras, under the key "data".
Use below code to retrieve image and displays it in an ImageButton.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageButton.setImageBitmap(imageBitmap);
}
}
Here is the full code:
import android.app.Activity;
import android.content.Intent;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RatingBar;
import android.widget.Toast;
public class AddPlayground extends AppCompatActivity {
EditText playGroundName;
RatingBar rate;
ImageButton imageButton;
private static final int REQUEST_IMAGE_CAPTURE = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_playground);
playGroundName = (EditText) findViewById(R.id.editText_playground_name);
rate = (RatingBar) findViewById(R.id.ratingBar);
imageButton = (ImageButton) findViewById(R.id.imageButton_add_playground_image);
Intent intent = getIntent();
Double message = intent.getDoubleExtra(MainActivity.EXTRA_MESSAGE, 0);
Toast.makeText(this, String.valueOf(message), Toast.LENGTH_SHORT).show();
}
public void addPhoto(View view) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
if(extras != null) {
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageButton.setImageBitmap(imageBitmap);
}
}
}
}
Make sure you have added below uses-feature tag in AndroidManifest.xml to use the Camera feature:
<manifest ... >
<uses-feature android:name="android.hardware.camera"
android:required="true" />
.......
..................
</manifest>
Hope this will help~

Loading images from gallery into ImageButton(view)

im looking to have two buttons on the same layout, you click the first button, chose an image and that button changes to that image chosen. You click the second button and that image chosen will replace the button. Easiest to use an imageButton instead of ImageView.
I would like the code if possible, thank you.
(Still dont understand? The end should be 2 images next to each other chosen by the user.)
MainActivity:
package com.example.triptych;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends Activity {
private static int RESULT_LOAD_IMG = 1;
String imgDecodableString;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void loadImagefromGallery(View view) {
// Create intent to Open Image applications like Gallery, Google Photos
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
&& null != data) {
// Get the Image from data
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
// Get the cursor
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
// Move to first row
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
ImageButton imageButton = (ImageButton) findViewById(R.id.buttonLoadPicture);
// Set the Image in ImageView after decoding the String
imageButton.setImageBitmap(BitmapFactory
.decodeFile(imgDecodableString));
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
.show();
}
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/buttonLoadPicture"
android:layout_width="160dp"
android:layout_height="300dp"
android:layout_marginLeft="10dp"
android:layout_weight="0.51"
android:contentDescription="TODO"
android:onClick="loadImagefromGallery"
android:src="#drawable/ic_launcher"
android:text="#string/load_picture" />
<ImageButton
android:id="#+id/button2"
android:layout_width="160dp"
android:layout_height="300dp"
android:layout_marginLeft="180dp"
android:layout_weight="0.51"
android:contentDescription="TODO"
android:onClick="loadImagefromGallery"
android:src="#drawable/ic_launcher"
android:text="#string/load_picture" />
</RelativeLayout>
Another question, do I create another activity for the second button and paste the same code or do I do it on the same activity?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/buttonLoadPicture"
android:layout_width="160dp"
android:layout_height="300dp"
android:layout_marginLeft="10dp"
android:layout_weight="0.51"
android:contentDescription="TODO"
android:onClick="loadImagefromGallery"
android:src="#drawable/ic_launcher"
android:text="#string/load_picture" />
<ImageButton
android:id="#+id/button2"
android:layout_width="160dp"
android:layout_height="300dp"
android:layout_marginLeft="180dp"
android:layout_weight="0.51"
android:contentDescription="TODO"
android:onClick="loadImagefromGallery"
android:src="#drawable/ic_launcher"
android:text="#string/load_picture" />
You can try something like this:
In your MainActivity class you need to create 2 different methods to handle gallery pick images , and call it in on click of two different image
buttons, see below code:
your MainActivity.java will look like this:
public class MainActivity extends ActionBarActivity {
private static int RESULT_LOAD_IMG = 1, RESULT_LOAD_IMG_TWO = 2;
String imgDecodableString, imgDecodableStringTwo;
ImageButton btn_load, btn_loadTwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_load = (ImageButton) findViewById(R.id.buttonLoadPicture);
btn_loadTwo = (ImageButton) findViewById(R.id.buttonLoad);
btn_loadTwo.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
loadImagefromGalleryTwo(btn_loadTwo);
}
});
btn_load.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
loadImagefromGallery(btn_load);
}
});
}
public void loadImagefromGallery(View view) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}
public void loadImagefromGalleryTwo(View view) {
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMG_TWO);
}
#SuppressLint("NewApi")
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == RESULT_LOAD_IMG && 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]);
imgDecodableString = cursor.getString(columnIndex);
cursor.close();
Drawable d = new BitmapDrawable(getResources(), BitmapFactory.decodeFile(imgDecodableString));
btn_load.setBackground(d);
} else if (requestCode == RESULT_LOAD_IMG_TWO && 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]);
imgDecodableStringTwo = cursor.getString(columnIndex);
cursor.close();
Drawable d = new BitmapDrawable(getResources(), BitmapFactory.decodeFile(imgDecodableStringTwo));
btn_loadTwo.setBackground(d);
}
} catch (Exception e) {
Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}
}
Output:
Hope it helps!
getContentResolver().query should not be called from UI thread

All the pictures from the gallery in a view -- Android

How can I make that all the pictures from my gallery appear in one view in Android Studio? I need all of my pictures to use after face recognition on them.
Here is the begining of my code:
package com.face.user.faceapplication;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
//import static android.widget.ImageView.*;
public class FirstClass extends Activity {
private static int RESULT_LOAD_IMAGE = 1;
// private ImageView image;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_activity);
Button button = (Button) findViewById(R.id.button);
// ImageView image = (ImageView) findViewById(R.id.image);
button.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);
}
});
}
private static int LOAD_IMAGE_RESULTS = 1;
#Override
//semmi
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.image);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
and the xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MyActivity">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:text="Click here" />
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
but if I run this I can just pick one photo from my gallery and I need all of them in a view.
you need to use putExtra on intent like this:
// ImageView image = (ImageView) findViewById(R.id.image);
button.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);
i.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
N.B. Works in API 18 and higher.

How to load Progress Bar Circle after user selects image from gallery in android?

I am doing an app in which user selects an image from gallery and goes to second activity with the selected image from gallery and displays it in second activity.But it takes some time(approx 3 sec) to go to second activity after user clicks on an image in gallery.I want to display progress bar circle for that much of time after the user selects an image from gallery and want to make progress bar circle invisible when execution moves to second activity.I am not getting any idea how to do my task?Should I use any AsyncTask?Please help me.I am stuck here.
I am providing my sample code.
My first Activity is
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
public class LauncherActivity extends Activity
{
private static int RESULT_LOAD_IMAGE = 2;
ImageButton gallery;
Bitmap bitmap_for_gallery;
protected void onCreate(Bundle paramBundle)
{
super.onCreate(paramBundle);
setContentView(R.layout.launcher);
gallery = (ImageButton)findViewById(R.id.select_photo);
gallery.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent gallery_intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(gallery_intent, RESULT_LOAD_IMAGE);
}
});
}
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();
ProgressDialog progress=new ProgressDialog(getApplicationContext());
progress.setIndeterminate(true);
progress.setTitle("Please wait");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.show();
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();
progress.dismiss();
Intent intent = new Intent(LauncherActivity.this, MainActivity.class);
intent.putExtra("path", picturePath);
startActivity(intent);
}
}
}
my first activity xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/homepage"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:scaleType="fitXY"/>
<ImageButton
android:id="#+id/select_photo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="126dp"
android:background="#android:color/transparent"
android:src="#drawable/select_photo" />
</RelativeLayout>
my second activity is
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout.LayoutParams;
public class MainActivity extends Activity {
ImageView background;
Bitmap transfered;
FrameLayout.LayoutParams layoutParams;
#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
background=(ImageView)findViewById(R.id.imageView1);
layoutParams=new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
Bundle extras = getIntent().getExtras();
String picturePath=extras.getString("path");
transfered=BitmapFactory.decodeFile(picturePath);
background.setImageBitmap(transfered);
background.setAdjustViewBounds(true);
background.setLayoutParams(layoutParams);
}
}
My second activity xml is
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.progressbarcircle.MainActivity"
tools:ignore="MergeRootFrame" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"
android:layout_gravity="center" />
</FrameLayout>
Thanks in advance.please help me.
If your first Activity is taking too much time create a ProgressDialog object and show() it after
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
and dismiss() it before startActivity(intent);
You can do the same for the second Activity if it takes too long.

Categories

Resources