Android Progress bar on ImageView - android

I am developing an app in which user will take Picture from camera and display it on preview screen having image view.
**CameraCapture.java**
class ButtonClickHandler implements View.OnClickListener
{
public void onClick( View view ){
myVib.vibrate(50);
startCameraActivity();
}
}
protected void startCameraActivity()
{
File filenew = new File( _path );
Uri outputFileUri = Uri.fromFile( filenew );
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
intent.putExtra( MediaStore.EXTRA_OUTPUT, outputFileUri );
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivityForResult( intent, 0 );
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch( resultCode )
{
case 0:
break;
case -1:
//pictaken++;
onPhotoTaken();
break;
}
}
protected void onPhotoTaken()
{
//pictaken=true;
Intent i = new Intent(CameraCapture.this,Preview.class);
startActivity(i);
}
In my Preview class the captured picture is displayed on ImageView
**Preview.java**
File imgFile = new File("/sdcard/DCIM/Camera/test.jpg");
if(imgFile.exists()){
// Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.image);
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
Matrix mat = new Matrix();
String degree="90";
mat.postRotate(Integer.parseInt(degree));
Bitmap bMapRotate = Bitmap.createBitmap(myBitmap, 0, 0,myBitmap.getWidth(),myBitmap.getHeight(), mat, true);
myImage.setImageBitmap(bMapRotate);
//myImage.setImageBitmap(myBitmap);
}
_image = ( ImageView ) findViewById( R.id.image );
Is there is any way to show progress bar on the ImageView till it load the Captured image from SD card.
Thnx in advance :)

Put your ImageView and Progressbar in a RelativeLayout. For your ProgressBar, use:
android:layout_centerInParent="true"
Which will center it in the RelativeLayout, meaning over the ImageView. You might also want to hide the ProgressBar when the image has loaded:
progressBar.setVisibility(View.Gone)
when the image has been loaded.
Sample code:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"/>
<ProgressBar
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="visible"/>
</RelativeLayout>

This is my solution. It is a little bit different. You can use it to create intro view with an image and progress bar. Here, progress bar is at 150dp to the bottom of center. FrameLayout can also be used.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/intro_description"
android:scaleType="fitXY"
android:src="#drawable/intro" />
<ProgressBar
android:id="#+id/progress"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="150dp"
android:visibility="visible" />
</FrameLayout>

Related

How to programmatically take screenshot of an android application without notification and navigation bars

I am using the below code for taking screenshot in my application.
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
rootView.buildDrawingCache(true);
Bitmap bitmap = rootView.getDrawingCache();
But I am getting an image with two black portion in the top and bottom by the notification and navigation bars. I need to remove those black areas. I need not to replace those area with the notification and navigation bars. But i need the toolbar.
take the drawing cache of the root Layout as below:
//onCreate
setContentView(R.layout.activity_main);
layout = (RelativeLayout) findViewById(R.id.activity_main);
layout.setDrawingCacheEnabled(true);
And to get bitmap, the usual way,
Bitmap bitmap = layout.getDrawingCache();
activity_main:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/colorPrimary" />
<!--other content-->
</RelativeLayout>
Use Below Method:
public static Bitmap loadBitmapFromView(Context context, View v) {
DisplayMetrics dm = context.getResources().getDisplayMetrics();
v.measure(View.MeasureSpec.makeMeasureSpec(dm.widthPixels, View.MeasureSpec.EXACTLY),
View.MeasureSpec.makeMeasureSpec(dm.heightPixels, View.MeasureSpec.EXACTLY));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Bitmap returnedBitmap = Bitmap.createBitmap(v.getMeasuredWidth(),
v.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(returnedBitmap);
v.draw(c);
return returnedBitmap;
}
Pass your parent view inside the argument.
Take this to another Linear Layout
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/munch"
android:id="#+id/munchscreen"/>
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="#+id/screenshots"
android:contentDescription="#string/app_name"
/>
</LinearLayout>
Now find Id for this Linear Layout on oncreate() method making it global variable
LinearLayout ll;
ll = (LinearLayout)findViewById(R.id.linear);
now capture this screen:
ll.setDrawingCacheEnabled(true);
ll.buildDrawingCache(true);
Bitmap cs = Bitmap.createBitmap(ll.getDrawingCache());
ll.setDrawingCacheEnabled(false);
Now set this bitmap to your ImageView.
Following is the XML layout file where I have added a button, textview and an imageview in a linear layout. And I have also added onClick attribute in the button.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_screenshoat"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.ncrypted.myapplication.Screenshoat">
<Button
android:id="#+id/btnScreenshot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Taake screen shot"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView"/>
</LinearLayout>
This is android activity file where I have added some java code to take screenshot and import android.graphics.Bitmap;, java.io.ByteArrayOutputStream;, java.io.File; etc.
public class Screenshoat extends AppCompatActivity {
Button btnScreenshot;
Bitmap mbitmap;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screenshoat);
btnScreenshot = (Button) findViewById(R.id.btnScreenshot);
imageView = (ImageView) findViewById(R.id.imageView);
btnScreenshot.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mbitmap = getBitmapOFRootView(v);
imageView.setImageBitmap(mbitmap);
createImage(mbitmap);
Toast.makeText(Screenshoat.this, "Screen shoat created", Toast.LENGTH_SHORT).show();
}
});
}
public Bitmap getBitmapOFRootView(View v) {
View rootview = v.getRootView();
rootview.setDrawingCacheEnabled(true);
Bitmap bitmap1 = rootview.getDrawingCache();
return bitmap1;
}
public void createImage(Bitmap bmp) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
File file = new File(Environment.getExternalStorageDirectory() +
"/capturedscreenandroid.jpg");
try {
file.createNewFile();
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(bytes.toByteArray());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Add Permission to read and write external storage
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Camera opens on clicking anywhere rather than on button

Image for button 500x500.
I have created an ImageButton and when ImageButton is clicked the system camera should launch and the image brought from the system's camera app will be shown in the ImageView. But, the problem is: the button doesn't work here. Other than button if I click anywhere in the app the system camera launches but in case of button click the camera doesn't launch. What kind of error is causing this unexpected behaviour?
Java File:
public class Home extends AppCompatActivity {
static final int REQUEST_IMAGE_CAPTURE = 1;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
// CAMERA BUTTON IMPLEMENTATION
ImageButton cameraButton = (ImageButton)findViewById(R.id.imageButton);
imageView = (ImageView)findViewById(R.id.imageView);
/* Disable the button if the user doesn't have camera */
if(!hasCamera())
cameraButton.setEnabled(false);
// CAMERA BUTTON IMPLEMENTATION
}
// Check if the user has a camera
private boolean hasCamera(){
return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
}
// Launching the camera
public void launchCamera(View view){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//Take a picture and pass results along to onActivityResult
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
// If you want to return the image taken
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
//Get the photo
Bundle extras = data.getExtras();
Bitmap photo = (Bitmap) extras.get("data");
imageView.setImageBitmap(photo);
}
}
}
XML FILE:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.muchmore.www.chasquido.Home"
android:background="#drawable/home_background"
android:id="#+id/home_activity"
android:onClick="launchCamera">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome User!!!"
android:id="#+id/welcome_tag"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textColor="#000"
/>
<ImageButton
android:layout_width="90dp"
android:layout_height="90dp"
android:id="#+id/imageButton"
android:background="#drawable/camera_button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:minHeight="300dp"
android:minWidth="300dp"
android:layout_above="#+id/imageButton"
android:layout_centerHorizontal="true" />
</RelativeLayout>
You have assigned OnClickListener android:onClick="launchCamera" to your RelativeLayout (your root view) instead to your ImageButton.

Show Camera image preview after click picture

I am making a custom camera like whats app. When i open the custom camera it show me screen same as wats app.
When i click the image, I need to show the preview like whats app in given below image.i does not know what mechanism WHATS App use to show the preview
*Note - Does whats app use a image view to set the bit map for showing picture preview or some other sophisticated way.
I am just giving you a working idea what WhatsApp doing in background in capture.
Result of answer, Change layout design as preferred, my design is basic.
camera_preview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center_horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/imageView"
android:layout_weight="1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_gravity="center_horizontal"
android:hint="Add a caption...."
android:textColorHint="#999999"
android:background="#333333"
android:padding="10dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="#+id/cancel"
android:layout_weight="1"
android:textColor="#ffffff" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Okay"
android:id="#+id/okay"
android:layout_weight="1"
android:textColor="#ffffff" />
</LinearLayout>
</LinearLayout>
and class Preview.java as
public class Preview extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.camera_preview);
ImageView image=(ImageView)findViewById(R.id.imageView);
Intent data=getIntent();
File imgFile = new File(data.getStringExtra("path"));
//Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screenWidth = size.x;
int screenHeight = size.y;
// Get target image size
Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
int bitmapHeight = bitmap.getHeight();
int bitmapWidth = bitmap.getWidth();
// Scale the image down to fit perfectly into the screen
// The value must be adjusted for phone/tables displays
while (bitmapHeight > (screenHeight ) && bitmapWidth > (screenWidth)) {
bitmapHeight = bitmapHeight / 2;
bitmapWidth = bitmapWidth / 2;
}
// Create resized bitmap image
BitmapDrawable resizedBitmap = new BitmapDrawable(getResources(),
Bitmap.createScaledBitmap(bitmap, bitmapWidth, bitmapHeight,
false));
image.setImageDrawable(resizedBitmap);
}
public void onCancelClick(View v){
}
public void onOkayClick(View v){
}
From your main activity startCamera by
public void startCamera(){
File file = new File(path);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 2);
}
declare String path as global veriable
Inside onCreate method
path = Environment.getExternalStorageDirectory() + "/example.jpg";
I am starting activity for result Instead of startring activity you
can just shows a dialog,
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 2 && resultCode == RESULT_OK) {
Intent startPreview = new Intent(this, Preview.class);
startPreview.putExtra("path", path);
startActivity(startPreview);
}
super.onActivityResult(requestCode, resultCode, data);
}

Can I display a image in custom layout?

I know I can use the below code to display a image in system layout,
but I hope to display an image in my custom layout, how can I do that? Thanks!
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + arrPath[id]), "image/*");
startActivityForResult(intent,ForBrowse);
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/border_ui"
android:orientation="vertical"
android:paddingTop="3dip" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:id="#+id/btnClose"
style="#style/myTextAppearance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/myreturn" />
</LinearLayout>
If the above resolutions are not ok, then try this one, it loads the image from tha page you want:
String url = "file://" + arrPath[id]), "image/*";
Bitmap bmp = fromURL(url);
imgview.setImageBitmap(bmp);
and write this function:
public static Bitmap fromURL(String src) {
try {
URL url = new URL(src);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap mybitmap = BitmapFactory.decodeStream(input);
return bitmap;
} catch (Exception exception1) {
return null;
}
after setting the xml up as you have done,
simply add the following code to set image to your imageview
ImageView img = (ImageView)findViewById(R.id.Imageview1);
img.setImageResource(R.drawable.yourimage);
Intent intent = new Intent(this, YourImageViewerActivity.class);
Bundle bundle = new Bundle();
bundle.putString("data", yourImageUri);
intent.putExtras(bundle);
startActivity(intent);
onCreate() method:
Bundle bundle = getIntent().getExtras();
String yourImageUri= bundle.getString("data");
Bitmap bitmap = BitmapFactory.decodeFile(yourImageUri);
ImageView myImageView = (ImageView)findViewById(R.id.imageView1);
myImageView.setImageBitmap(bitmap);
If you are trying to add an image to you layout from the XML file then
<ImageView
android:id="#+id/imageView1"
android:src="#drawable/book" <- Change this to your drawable item
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />

How to load thumbnail from custom directory?

I have searched a lot but I got samples to fetch thumbnail from an Url or from Http,
but I actually need to get thumbnail of an image from my own directory on sdcard withot accesing the Android default thumbnail directory, thats is I need to fetch a thumbnail of an image from
mnt/sdcard/myown/1.png
Can anybody help for this please?
Thanks in advance.
Environment.getExternalStorageDirectory() returns "/mnt/sdcard"
String imagePath = Environment.getExternalStorageDirectory().toString() + PATH_TO_IMAGE;
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
#Override
public void onCreate(Bundle savedInstanceState) {
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(onClickListener);
image = (ImageView) findViewById(R.id.imageView1);
}
private OnClickListener onClickListener = new OnClickListener() {
#Override
public void onClick(final View v) {
switch(v.getId()){
case R.id.button1:
String imagePath = Environment.getExternalStorageDirectory().toString() +"/myown/1.png";
Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
image.setImageBitmap(bitmap);
break;
}
}
};
your layout.xml
<?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" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/android" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change Image" />
</LinearLayout>

Categories

Resources