I'm still new here in android. Please help me. I manage to put the imageview to the left and right side of my Layout. My problem is when I select an image for imageview2 it passes the image to imageview1 and still the imageview2 can be seen from the right side.
I need to do is when I select an image for imageview2 it should be fixed at the right side. I think I have problems with my code in java?
Here is my code for xml.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_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=".MainActivity" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/insert_bg"
>
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="15dip"
android:paddingBottom="15dip"
>
<ImageView
android:id="#+id/img_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fitsSystemWindows="false"
android:scaleType="fitStart"
android:src="#drawable/insert_ci" />
<ImageView
android:id="#+id/img_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:fitsSystemWindows="false"
android:scaleType="fitEnd"
android:src="#drawable/insert_ci"
android:textAlignment="viewEnd" />
</LinearLayout>
And this code for java.
package com.prototype;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
//import android.widget.ImageButton;
import android.widget.ImageView;
//import android.widget.LinearLayout;
public class MainActivity3 extends Activity {
private static final int SELECT_PICTURE = 2;
private String selectedImagePath;
private String selectedImagePath2;
private ImageView img;
private ImageView img2;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.open_project);
img = (ImageView)findViewById(R.id.img_left);
img2= (ImageView)findViewById(R.id.img_center);
addImageViewClickListener();
addImageView2ClickListener();
}
public void addImageViewClickListener()
{
ImageView btnNavigator1 = (ImageView)findViewById(R.id.img_left);
btnNavigator1.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select character image for right side."), SELECT_PICTURE);
}
//#Override
//public void onClick(View v) {
// TODO Auto-generated method stub
//}
});
}
public void addImageView2ClickListener()
{
ImageView btnNavigator2 = (ImageView)findViewById(R.id.img_center);
btnNavigator2.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select character image for left side."), SELECT_PICTURE);
}
//#Override
//public void onClick(View v) {
// TODO Auto-generated method stub
//}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if (requestCode == SELECT_PICTURE)
{
//File folder = new File(Environment.getExternalStorageDirectory() + "/Database/");
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}
}
}
public void onActivityResult2(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if (requestCode == SELECT_PICTURE)
{
//File folder = new File(Environment.getExternalStorageDirectory() + "/Database/");
Uri selectedImageUri = data.getData();
selectedImagePath2 = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath2);
img2.setImageURI(selectedImageUri);
}
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
The problem that I was talking about is like this.http://imageshack.us/photo/my-images/31/6sa6.png/
printscreen image
Use this for two imageViews. So your images will not overlap
android:adjustViewBounds="true"
android:scaleType="fitXY
Try this one,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/img_left"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fitsSystemWindows="false"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/img_center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fitsSystemWindows="false"
android:scaleType="fitXY"
android:src="#drawable/ic_launcher"
android:textAlignment="viewEnd" />
</LinearLayout>
The image is getting stretched because of the weight mentioned.
The android:layout_weight for the first imageView is 1 and that is for second imageView is 2.
This leads to taking 2/3rd of the screen by the the second imageView and the rest by the first.
Adjust the weights and you can overcome the issue.
Change your Imageview make 0dp to your android:layout_width#
<ImageView
android:id="#+id/img_left"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fitsSystemWindows="false"
android:scaleType="fitStart"
android:src="#drawable/icon" />
<ImageView
android:id="#+id/img_center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:fitsSystemWindows="false"
android:scaleType="fitEnd"
android:src="#drawable/icon"
android:textAlignment="viewEnd" />
Update
Create one more folder named layout-land and keep same xml inside with appropriate changes, hope it works.
Related
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>
ERROR LINE:java.lang.NullPointerException: Attempt to invoke virtual
method 'void android.widget.ImageView.setOnClickListener(android.view.View$OnClickListener)'
on a null object reference
.JAVA FILE
package co.hangyr.Hangyr.Activity.Camera;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import butterknife.ButterKnife;
import butterknife.InjectView;
import co.hangyr.Hangyr.Activity.Home.ActivityHome;
import co.hangyr.Hangyr.Activity.Others.Footer;
import co.hangyr.Hangyr.R;
public class ActivityCamera extends Footer implements View.OnClickListener {
static final int RESULT_LOAD_IMAGE = 1;
private Camera mCamera;
private CameraPreview mCameraPreview;
ImageView imageView;
/* static final int SECOND_PIC = 2;
static final int THIRD_PIC = 3;
static final int FORTH_PIC = 4;
static final int FIFTH_PIC = 5;
*/
#InjectView(R.id.ivFirstPic)
ImageView firstPic;
#InjectView(R.id.ivCapture)
ImageView ivCapture;
#InjectView(R.id.ivFlipCamera)
ImageView flipCamera;
#InjectView(R.id.ivFlash)
ImageView flash;
#InjectView(R.id.tvUploadFromGallery)
TextView tvUploadFromGallery;
#InjectView(R.id.ivSecondPic)
ImageView secondPic;
#InjectView(R.id.ivThirdPic)
ImageView thirdPic;
#InjectView(R.id.ivForthPic)
ImageView forthPic;
#InjectView(R.id.ivFifthPic)
ImageView fifthPic;
#InjectView(R.id.ivJustClicked)
FrameLayout justClicked;
// #InjectView(R.id.tvNextEditPic)
TextView tvNext;
#InjectView(R.id.bDeleteFirstPic)
Button deleteFirstPic;
#InjectView(R.id.bDeleteSecondPic)
Button deleteSecondPic;
#InjectView(R.id.bDeleteThirdPic)
Button deleteThirdPic;
#InjectView(R.id.bDeleteForthPic)
Button deleteForthPic;
#InjectView(R.id.bDeleteFifthPic)
Button deleteFifthPic;
#InjectView(R.id.tvCancel)
TextView tvCancel;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
ButterKnife.inject(this);
tvNext = (TextView) findViewById(R.id.tvNextEditPic);
tvNext.setOnClickListener(this);
mCamera = getCameraInstance();
mCamera.setDisplayOrientation(90);
mCameraPreview = new CameraPreview(this, mCamera);
justClicked.addView(mCameraPreview);
ivCapture.setOnClickListener(this);
tvUploadFromGallery.setOnClickListener(this);
firstPic.setOnClickListener(this);
deleteFirstPic.setOnClickListener(this);
deleteSecondPic.setOnClickListener(this);
deleteThirdPic.setOnClickListener(this);
deleteForthPic.setOnClickListener(this);
deleteFifthPic.setOnClickListener(this);
tvCancel.setOnClickListener(this);
}
private Camera getCameraInstance() {
Camera camera = null;
try {
camera = Camera.open();
flipCamera.setVisibility(View.VISIBLE);
flash.setVisibility(View.VISIBLE);
} catch (Exception e) {
// cannot get camera or does not exist
}
return camera;
}
Camera.PictureCallback mPicture = new Camera.PictureCallback() {
#Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile();
if (pictureFile == null) {
return;
}
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
Bitmap bmp = BitmapFactory.decodeByteArray(data, 0, data.length);
firstPic.setImageBitmap(bmp);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
};
private static File getOutputMediaFile() {
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
"MyCameraApp");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
return mediaFile;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.ivCapture:
mCamera.takePicture(null, null, mPicture);
break;
case R.id.ivFirstPic:
mCameraPreview.getHolder().removeCallback(mCameraPreview);
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
deleteFirstPic.setVisibility(View.VISIBLE);
break;
case R.id.tvCancel:
startActivity(new Intent(getApplicationContext(), ActivityHome.class));
break;
case R.id.tvNextEditPic:
startActivity(new Intent(getApplicationContext(), UploadPic.class));
break;
case R.id.bDeleteFirstPic:
firstPic.setImageResource(R.drawable.pic_item_cam);
deleteFirstPic.setVisibility(View.GONE);
break;
case R.id.bDeleteSecondPic:
secondPic.setImageResource(R.drawable.pic_item_cam);
deleteSecondPic.setVisibility(View.GONE);
break;
case R.id.bDeleteThirdPic:
thirdPic.setImageResource(R.drawable.pic_item_cam);
deleteThirdPic.setVisibility(View.GONE);
break;
case R.id.bDeleteForthPic:
forthPic.setImageResource(R.drawable.pic_item_cam);
deleteForthPic.setVisibility(View.GONE);
break;
case R.id.bDeleteFifthPic:
fifthPic.setImageResource(R.drawable.pic_item_cam);
deleteFifthPic.setVisibility(View.GONE);
}
}
#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();
firstPic.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}
.XML FILE
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/backgroundApp"
android:orientation="vertical"
android:weightSum="2">
<LinearLayout
android:id="#+id/llNameBack"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_marginBottom="1dp"
android:background="#color/white"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/tvCancel"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_gravity="left|center"
android:layout_marginBottom="15dp"
android:paddingLeft="15dp"
android:layout_marginTop="14dp"
android:layout_weight="1"
android:gravity="left|center_vertical"
android:text="#string/cancelS"
android:textColor="#color/black"
android:textSize="13dp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvNextEditPic"
android:layout_width="match_parent"
android:layout_height="48dp"
android:layout_gravity="end|center_vertical"
android:layout_marginBottom="15dp"
android:paddingRight="15dp"
android:layout_marginTop="14dp"
android:layout_weight="1"
android:background="#color/white"
android:gravity="right|center_vertical"
android:text="#string/nextS"
android:textColor="#fd676a"
android:textSize="13dp"
android:textStyle="bold" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="375dp">
<FrameLayout
android:id="#+id/ivJustClicked"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:layout_weight="1">
<ImageView
android:id="#+id/ivFlipCamera"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left|bottom"
android:padding="8dp"
android:src="#drawable/flip_camera" />
<ImageView
android:id="#+id/ivFlash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:padding="8dp"
android:src="#drawable/flash_icon" />
</FrameLayout>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical">
<TextView
android:id="#+id/tvUploadFromGallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/white"
android:padding="18dp"
android:text="#string/galleryS"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#4a4a4a"
android:textSize="13dp"
android:textStyle="bold" />
<ImageView
android:id="#+id/ivCapture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="16dp"
android:layout_marginTop="-30dp"
android:src="#drawable/capture" />
<include
layout="#layout/camera_pic_added"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="1dp"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
You should use #Bindinstead of #InjectView
#InjectView was replaced June 2015.
You missed to initialize this variable ivFirstPic:
ivFirstPic.setOnClickListener(this);
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.
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.
making a simple app, the problem I am having now is that when im using an intent to launch the camera app to take a picture, the app is supposed to take the picture and overwrite the imageview which was for the background image. What its doing is bring the freshly taken picture and displaying it sideways.
this is my main java file:
package com.example.piktuur;
import java.io.File;
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.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
public class PiktuurMain extends Activity {
private Uri outputFileUri;
private static int TAKE_PICTURE = 1;
private ImageView image;
private SeekBar adjust;
public int height, width;
public boolean proceede;
public File file;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.piktuur_main);
image = (ImageView) findViewById(R.id.imageView1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.piktuur_main, menu);
return true;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) {
BitmapFactory.Options options = new BitmapFactory.Options();
String file = Environment.getExternalStorageDirectory() + "/image.png";
Bitmap bitmap = BitmapFactory.decodeFile(file, options);
image.setImageBitmap(bitmap);
}
}
public void cameraClicked(View V){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(), "image.png");
outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, TAKE_PICTURE);
}
public void shareClicked(View V){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, "stephen.w.protzman#gmail.com");
intent.putExtra(Intent.EXTRA_SUBJECT, "Piktuur");
intent.putExtra(Intent.EXTRA_TEXT, "Here is a picture for you!");
startActivity(Intent.createChooser(intent, "Send Email"));
}
}
and here is the main xml with the imageview in it:
<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"
tools:context=".PiktuurMain" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="fitXY"
android:src="#drawable/pic" />
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="5dp" >
<ImageButton
android:id="#+id/takepic"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#drawable/round"
android:onClick="cameraClicked"
android:src="#android:drawable/ic_menu_camera" />
<ImageButton
android:id="#+id/editpic"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_weight="1"
android:background="#drawable/round"
android:src="#android:drawable/ic_menu_edit" />
<ImageButton
android:id="#+id/sharepic"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:background="#drawable/round"
android:onClick="shareClicked"
android:src="#android:drawable/ic_menu_share" />
</LinearLayout>
<SeekBar
android:id="#+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/linearLayout1"
android:layout_alignParentLeft="true" />
</RelativeLayout>
I have zero ideas why this is happening any clue?
try this
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK)
{
Uri imageUri = data.getData();
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
image.setImageBitmap(bitmap);
}
}