How to load thumbnail from custom directory? - android

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>

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" />

can anyone guide How to set search button

I'm developing an app of wallpapers. In it I have more than 100 images. I'm using 2 buttons to change an image to the next one and backward. Now I want to use a third button which will open the complete list of the names of drawable images and this list should move up and down. If I select the desired image name then it should open that image.
This list also has 2 buttons on top of it, one to go to the image and one to cancel. Here is what it would look like:
Here is my XML layout.
<?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:gravity="fill"
android:orientation="vertical"
android:weightSum="100" >
<ImageView
android:id="#+id/idImageViewPic"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="100"
android:adjustViewBounds="true"
android:background="#66FFFFFF"
android:maxHeight="91dip"
android:maxWidth="47dip"
android:padding="10dip"
android:src="#drawable/r0" />
<LinearLayout android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/bprev"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Back" >
</Button>
<Button
android:id="#+id/bnext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Next" >
</Button>
</LinearLayout>
</LinearLayout>
My Main Activity:
public class Main extends Activity {
private ImageView hImageViewPic;
private Button iButton, gButton;
private int currentImage = 0;
int[] images = { R.drawable.r1, R.drawable.r2, R.drawable.r3, R.drawable.r4, R.drawable.r5, R.drawable.r6, R.drawable.r7, R.drawable.r8, R.drawable.r9, R.drawable.r10 };
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hImageViewPic = (ImageView)findViewById(R.id.idImageViewPic);
iButton = (Button) findViewById(R.id.bnext);
gButton = (Button) findViewById(R.id.bprev);
//Just set one Click listener for the image
iButton.setOnClickListener(iButtonChangeImageListener);
gButton.setOnClickListener(gButtonChangeImageListener);
}
View.OnClickListener iButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
//Increase Counter to move to next Image
currentImage++;
currentImage = currentImage % images.length;
hImageViewPic.setImageResource(images[currentImage]);
}
};
View.OnClickListener gButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
//Increase Counter to move to next Image
currentImage--;
currentImage = currentImage % images.length;
hImageViewPic.setImageResource(images[currentImage]);
}
};}
If I use another button in XML then what should I write in the Main Activity? Is there a better way to do this?
i think , what you can do is :
you have list of drawables/bitmaps device .
Drawable myDrawable = getResources().getDrawable(R.drawable.yourImage); // Drawables
Bitmap myLogo = ((BitmapDrawable) myDrawable).getBitmap();
imageView.setImageBitmap(myLogo);
if images is selected from device , or you have paths :
Uri selectedImage = convert your image path into URI ;
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
userImg.setImageBitmap(bitmap);
Edited :
At last , but before this line ,
ImageViewPic.setImageResource(images[currentImage]) ;
Add this line :
Drawable myDrawable = getResources().getDrawable(R.drawable.yourImage); // Drawables
Bitmap currentImage= ((BitmapDrawable) myDrawable).getBitmap();
then add
ImageViewPic.setImageResource(images[currentImage]) ;

Android ImageView is not bring to front

android ImageView is not bringing to front in RelativeLayout
Tried to call bringtofront() didn't work
tried setting visibility to true didn't work
XML CODE
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is /res/layout/main.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spellingLevel1Layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/canvas_container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/magnetboard" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="480dp"
android:layout_marginTop="600dp"
android:adjustViewBounds="true" />
</RelativeLayout>
</LinearLayout>
Java Code
public class spellingLevel1 extends Activity {
rel1 = (RelativeLayout) findViewById(R.id.canvas_container);
mImageView1 = (ImageView) findViewById(R.id.imageView1);
image1();
}
public void randomNumber() {
Random rando = new Random();
currentLetter = myImageList[rando.nextInt(myImageList.length)];
}
public void image1(){
//randomNumber();
mImageView1 = (ImageView) findViewById(R.id.imageView1);
mImageView1.setBackgroundResource(R.drawable.spelling_);
mImageView1.bringToFront();
mImageView1.requestLayout();
}
UPDATED CODE
rel1 = (RelativeLayout) findViewById(R.id.canvas_container);
mImageView1 = (ImageView) findViewById(R.id.imageView1);
image1();
}
public void randomNumber() {
Random rando = new Random();
currentLetter = myImageList[rando.nextInt(myImageList.length)];
}
public void image1(){
//randomNumber();
mImageView1 = (ImageView) findViewById(R.id.imageView1);
mImageView1.setImageResource(R.drawable.spelling_); //updated here to setImageResource
mImageView1.bringToFront();
Still does not show
its not the full code but it should be everything to help you see what is included in here
You should define the image source of the ImageView.
because it's size is set to wrap_content and it has no content, the size stays 0.

Android Progress bar on ImageView

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>

Display image after button click

I am making a really simple android application that will display a picture after a button is clicked. I have searched for days and tried everything I can think of but cant seem to get it to work. I have been able to host the picture online and link to it but I want the content available offline too. Please help, sorry for the stupid question.
Update:
It is a fixed image and in my drawable resource. Here is the current code I am using to display the image from a URL. What changes should I make to to display that same image from my drawable resource?
JAVA
public class StandingOrders extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void buttonClick (View image)
{
Uri uri = Uri.parse("my url");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent)
}
}
XML
<Button android:id="#+id/btn1"
android:layout_height="wrap_content"
android:layout_width="170px"
android:text="The Button"
android:layout_gravity="center"
android:clickable="true"
android:onClick="buttonClick">
</Button>
If it's a fixed image, include it as a drawable resource and use an ImageView to display it. If it's an image to download, you can do that in a separate thread and store it in a file or an SQLite data base, then again use an ImageView to see it.
Add an ImageView to your xml layout and call setImageResource from onClick.
setImageResource(R.drawable.yourImage);
Or you could set the image in your layout and make the ImageView hidden until you click the button. See setVisibility(View.GONE)
Check out this answer: https://stackoverflow.com/a/4896272/458968
In your case, the uri should be
Uri.parse("android.resource://com.company.app/"+R.drawable.my_image);
You can try to implement the method, which I am posting below. I have done something more or less similar to your requirement.
Method:
//method to zoom images
public void zoomImage(String imageUrl)
{
AlertDialog.Builder builder;
Context mContext = ExamActivity.this;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
//to close dialog
ImageView close_dialog = (ImageView) layout.findViewById(R.id.imageView_custom_dialog_close);
close_dialog.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
alertDialog.dismiss();
}
});
//to show image
WebView wv=(WebView) layout.findViewById(R.id.show_image_webView);
wv.getSettings().setBuiltInZoomControls(true);
wv.setInitialScale(200);
wv.loadUrl(imageUrl);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
alertDialog.show();
}
the imageUrl is the url to the webpage containing the image. If you want to show a local image then just create a simple html page containing the image and use the local url.
Try this:-
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imageToDisplay = (ImageView) findViewById(R.id.imageToDisplay);
Button btnShowImage = (Button) findViewById(R.id.btnShowImage);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
imageToDisplay.setVisibility(View.VISIBLE);
imageToDisplay.setImageResource(R.drawable.imagelogo);
}
});
}
}
In layout imageToDisplay's visibility is gone.
try following example
you can download "android-query-0.22.10.jar" file from following url.
http://www.java2s.com/Code/Jar/a/Downloadandroidquery02210jar.htm
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.addbuttondynamic.AddButtonDynamic" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load Image" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
and Main.java
import com.androidquery.AQuery;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class Main extends Activity {
Button btnLoad;
ImageView ivImage;
AQuery aQuery;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnLoad=(Button)findViewById(R.id.button1);
ivImage=(ImageView)findViewById(R.id.imageView1);
aQuery=new AQuery(this);
btnLoad.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//load image from /res folder.
ivImage.setImageResource(R.drawable.ic_launcher);
//load image from url.
aQuery.id(ivImage).image("you URL");
// if ivImage is not working in aQuery.id() then use R.id.imageView1.
}
});
}
}
activity_main.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="com.example.imagechange.MainActivity"
>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:text="#string/khilan"
android:onClick="btnOnClick"
/>
<ImageView
android:id="#+id/imageView1"
android:layout_width="800px"
android:layout_height="500px"
android:layout_marginTop="70dp"/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btnOnClick (View view)
{
String imagename = "khilan";
int res = getResources().getIdentifier(imagename, "drawable", getPackageName());
ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setImageResource(res);
}
}
Create a layout with image and button, make the image hidden and onClick set the visibility to VISIBLE.
Here is what the XML should look like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image"
android:src="#drawable/image1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"/>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Image" />
</RelativeLayout>
and the following is the Java code
ImageView image = findViewById(R.id.image);
Button button = findViewById(R.id.got_it_button);
image.setVisibility(View.GONE);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
image.setVisibility(View.VISIBLE);
}
});
Good luck :)

Categories

Resources