How can i make a button as Set Wallpaper button? - android

The code contains a gallery with one button (which has to be added into your manifest file), I just need a suggestion on the button action; what to put inside "imageIDs* or bitmap to reference to the image which previously is clicked and shown.
Here is the complete code:
package net.keivan.gallery;
import java.io.IOException;
import android.app.Activity;
import android.app.ProgressDialog;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
public class GalleryActivity extends Activity {
//---the images to display---
Integer[] imageIDs = {
R.drawable.pic1,
R.drawable.pic2,
R.drawable.pic3,
R.drawable.pic4,
R.drawable.pic5,
R.drawable.pic6,
R.drawable.pic8,
R.drawable.pic9,
R.drawable.pic10,
};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View v,
int position, long id)
{
//---Displays the name of images as just as i click on them---
Toast.makeText(getBaseContext(),
"pic" + (position + 1) + " selected",
Toast.LENGTH_SHORT).show();
//---display the images selected---
ImageView imageView = (ImageView) findViewById(R.id.image1);
imageView.setImageResource(imageIDs[position]);
}
});
}
//---Set Wallpaper button---
public void onClick(View v) {
try {
/*at this part can you suggest what to put inside "imageIDs* or *bitmap* to reference
to the image which previously is clicked and shown*/
/* first.the activity is created second.the image which i clicked is shown third.whin
the user click on the button the image which previously is clicked on is set as background.*/
WallpaperManager.getInstance(this).setResource(imageIDs[position]);
//WallpaperManager.getInstance(this).setBitmap(mBitmap);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
///////////////////////////////////////////////
public class ImageAdapter extends BaseAdapter
{
Context context;
int itemBackground;
public ImageAdapter(Context c)
{
context = c;
//---setting the style---
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
{ getApplicationContext().getWallpaper(); }
//---returns the number of images---
public int getCount() {
return imageIDs.length;
}
//---returns the item---
public Object getItem(int position) {
return position;
}
//---returns the ID of an item---
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(context);
imageView.setImageResource(imageIDs[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
} else {
imageView = (ImageView) convertView;
}
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}

try {
// Set background from a resource
WallpaperManager.getInstance(this).setResource(imageIDs[position]);
// or set background from a bitmap
//WallpaperManager.getInstance(this).setBitmap(mBitmap);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
In your manifest file:
<uses-permission android:name="android.permission.SET_WALLPAPER"></uses-permission>

Related

Grid view to stay intact once imageview image is changed

I am developing a game in Android where in the alpha value is changed once the image in the imageview and the image in the grid view match. If the number of matches are equal to 4, I need to change the image in the imageview and again compare with the images in the grid view.
I change the image in the imageview using imageView.setImageResource(q);
The image gets changed but the grid view disappears. Could you please tell me what I need to do for the grid view to change intact?
I tried gridView.invalidateViews(); but in vain. I also created a new imageadapter with the same context but it still did not work. Any help would be really great!
package com.example.despicablemehunt;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
final public Integer[] mThumbIds = {
R.drawable.icon1, R.drawable.icon2,
R.drawable.icon3, R.drawable.icon4,
R.drawable.icon1, R.drawable.icon2,
R.drawable.icon3, R.drawable.icon4,
R.drawable.icon1, R.drawable.icon2,
R.drawable.icon3, R.drawable.icon4,
R.drawable.icon1, R.drawable.icon2,
R.drawable.icon3, R.drawable.icon4
};
int i=0;
int p=0;
final Integer q=0;
public Context mContext;
List<Integer> imagesArrayList = new ArrayList<Integer>(Arrays.asList(mThumbIds));
Integer [] tempArrayList;
public static final Random rgenerator = new Random();
public static final Integer[] mImageIds =
{ R.drawable.icon1, R.drawable.icon2, R.drawable.icon3,R.drawable.icon4 };
#Override
protected void onCreate(Bundle savedInstanceState) {
final long time= System.currentTimeMillis();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Collections.shuffle(imagesArrayList);
tempArrayList = (Integer[])(imagesArrayList.toArray(new Integer[imagesArrayList.size()]));
final GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
final Integer q = mImageIds[rgenerator.nextInt(mImageIds.length)];
final ImageView iv = (ImageView) findViewById(R.id.imageView1);
iv.setImageResource(q);
gridView.setOnItemClickListener(new OnItemClickListener() {
#SuppressLint("NewApi")
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
if(getResources().getDrawable(q).getConstantState().equals(getResources().getDrawable(tempArrayList[arg2]).getConstantState()))
{
if(p==5)
{
/*final long time1= System.currentTimeMillis();
if ((time1-time)> 20)
{
}*/
Toast.makeText(getApplicationContext(), "DONE DONE DONE", Toast.LENGTH_SHORT).show();
}
arg1.setAlpha((float) 0.4);
if((++i)== 4)
{
gridView.setAlpha((float)0.0);
p++;
final Integer q = mImageIds[rgenerator.nextInt(mImageIds.length)];
ImageView imageView = (ImageView) iv;
imageView.setImageResource(q);
i=0;
gridView.invalidate();
}
}
}
});
}
#Override
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;
}
public class ImageAdapter extends BaseAdapter {
// Keep all Images in array
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(tempArrayList[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(50, 50));
return imageView;
}
}
}
To refresh the gridview you have to use gridView.invalidateViews(); So change it
after 4 correct click on gridview image why are you setting alpha 0 in gridview. So just remove the line
gridView.setAlpha((float)0.0);
//setting alpha causing the gridview invisible
so your code should be like this
if ((++i) == 4) {
p++;
final Integer q = mImageIds[rgenerator.nextInt(mImageIds.length)];
ImageView imageView = (ImageView) iv;
iv.setImageResource(q);
i = 0;
gridView.invalidateViews();
}
hope this will help
Thanks

How to save images view from GridView on SD Card

As i am new in android so i have created one project in that i just want to save the image view which comes from GridView, means after clicking on image it will get displayed so i want to save that image on SD card.
public class AndroidGridViewActivity extends Activity {
private static final String TAG = "AndroidGridViewActivity";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = (GridView) findViewById(R.id.grid_view);
gridView.setAdapter(new ImageAdapter(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent i = new Intent(getApplicationContext(),
Full_Image_Activity.class);
i.putExtra("id", position);
startActivity(i);
Log.v(TAG, "Clicked on photo");
}
});
}
}
public class Full_Image_Activity extends Activity {
private static final String TAG = "Full_Image_Activity";
Button save;
Bitmap bm;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
Intent i = getIntent();
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
Log.v(TAG, "Image Opened...");
}
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public Integer[] mThumbIds = { R.drawable.imagesa, R.drawable.imagesb,
R.drawable.imagesc, R.drawable.imagesd, R.drawable.imagese,
R.drawable.imagesf, R.drawable.imagesg, R.drawable.imagesh,
R.drawable.imagesi, R.drawable.imagesj, R.drawable.imagesk,
R.drawable.imagesl };
public ImageAdapter(Context c) {
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
}
}
Please try to use bellow code.
// this code write on Adapters getView method.
imageView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Drawable drawable = v.getDrawable();
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
String path=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File file = new File(path, "name.png");
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.close();
}
});
Please try to check this,this code is checked in my end.
// please add writeExternalStorage uses permision in android manifest.
// Activity
// code
package com.example.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class AndroidGridViewActivity extends Activity {
private static final String TAG = "AndroidGridViewActivity";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid);
GridView gridView = (GridView) findViewById(R.id.gridview);
gridView.setAdapter(new ImageAdapter(this));
}
}
class ImageAdapter extends BaseAdapter {
private Context mContext;
public Integer[] mThumbIds = { R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher, R.drawable.ic_launcher,
R.drawable.ic_launcher };
public ImageAdapter(Context c) {
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ImageView imageView = (ImageView) v;
Drawable drawable = imageView.getDrawable();
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
File path = Environment.getExternalStorageDirectory();
File file = new File(path, "name.png");
FileOutputStream out = null;
try {
out = new FileOutputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
return imageView;
}
}

Saving image to SD card not working

Having a problem at the bottom, I'm trying to create a case to save the image to SD card yet it isn't working and I don't know why.
I think it's got something to do with the getRawResource but still not sure.
Thanks for any help!!!
package com.nk_apps.random;
import java.io.InputStream;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
#SuppressWarnings("deprecation")
public class Main extends Activity {
private Gallery gallery;
private ImageView imgView;
private Integer[] imgId = {
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4,
R.drawable.image5,
R.drawable.image6,
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgView = (ImageView)findViewById(R.id.imageView01);
imgView.setImageResource(imgId[0]);
gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
imgView.setImageResource(imgId[position]);
}
});
}
public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;
public AddImgAdp(Context c) {
cont = c;
TypedArray typArray = obtainStyledAttributes(R.styleable.Gallery1);
GalItemBg = typArray.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
typArray.recycle();
}
public int getCount() {
return imgId.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imgView = new ImageView(cont);
imgView.setImageResource(imgId[position]);
imgView.setLayoutParams(new Gallery.LayoutParams(170, 270));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(GalItemBg);
return imgView;}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.bsaveImage:
InputStream inps = getResources().openRawResource(GalItemBg);
Bitmap bmp = BitmapFactory.decodeStream(inps);
try{
MediaStore.Images.Media.insertImage(getContentResolver(), bmp, "Title" , "Hip-Hop Wallpapers");
Toast saveToast = Toast.makeText(Main.this, "The image has been saved.", Toast.LENGTH_SHORT);
saveToast.show();
}catch(Exception e){
e.printStackTrace();
}
break;
}
}
} }
Did you verify you have write permission to sdcard ? (this is WRITE_EXTERNAL_STORAGE)
Also, you should make sure you look at the right mount point to find the image (can be "internal sdcard" for some devices, for example).

Android gallery display images from SDcard

this is my gallery program,in which i am displaying the content with static files,how to modify this program to show the photos in the SDcard,if there are no photos or SD card give an alert
package DisplayViewsExample.com;
import android.app.Activity;
import android.os.Bundle;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class DisplayViewsExampleActivity extends Activity {
Integer[] imageIDs = {
R.drawable.sample_1,
R.drawable.sample_2,
R.drawable.sample_3
};
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView parent,
View v, int position, long id)
{
/* Toast.makeText(getBaseContext(),
"pic" + (position + 1) + " selected",
Toast.LENGTH_SHORT).show();*/
ImageView imageView = (ImageView) findViewById(R.id.image1);
imageView.setImageResource(imageIDs[position]);
}
});
}
public class ImageAdapter extends BaseAdapter
{
private Context context;
private int itemBackground;
public ImageAdapter(Context c)
{
context = c;
//---setting the style---
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(
R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
//---returns the number of images---
public int getCount() {
return imageIDs.length;
}
//---returns the ID of an item---
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(imageIDs[position]);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 120));
imageView.setBackgroundResource(itemBackground);
return imageView;
}
}
}
You should set permission in your Manifest first:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Then, write the function which recursively fetches all your image files from SD:
final static String[] IMAGE_FORMATS = new String[] {
".JPEG", ".GIF", ".PNG", ".BMP"};
public static ArrayList<File> getImageFilesFromSD() {
File dir = Environment.getExternalStorageDirectory();
if (dir == null || !dir.exists())
return null;
ArrayList<File> images = getImageFilesFromDir(dir);
return images;
}
private static ArrayList<File> getImageFilesFromDir(File dir) {
File[] files = dir.listFiles();
ArrayList<File> result = new ArrayList<File>();
if (files != null)
for (File file : files) {
if (file.isDirectory()) {
result.addAll(getImageFilesFromDir(file));
} else {
final String fileName = file.getName();
final String fileNameLwr = fileName.toLowerCase();
// filtering images by extension
boolean contains = false;
for (int i = 0; i < IMAGE_FORMATS.length; i++) {
final String formatLwr = IMAGE_FORMATS[i].toLowerCase();
if (fileNameLwr.endsWith(formatLwr)) {
contains = true;
break;
}
}
if (contains) {
result.add(file);
}
}
}
return result;
}
After that in your list adapter:
imageView.setImageURI(Uri.fromFile(file));
where file is the element of ArrayList returned by getImageFilesFromSD()function.
Good luck.
The
Environment.getExternalStoragePublicDirectory(String type)
function gives you access to the external extorage. There are even contants for the most usual standard folder names.
Don't forget to put the permission to access it on the manifest.

how to display the next images by clicking on the "next" button

I am trying to display the image by clicking on the "next" button but this code doesn't work.
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
public class MyGridView extends Activity {
GridView mGridView;
int counter,i;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.mygridacti);
mGridView = (GridView)findViewById(R.id.grdvw);
//mGridView.setAdapter(new MyImageAdapter(this));
ImageView imageview = (ImageView)findViewById(R.id.GalleryView);
Button btnnext = (Button)findViewById(R.id.btn_nxt);
Log.i("counter", ""+counter);
btnnext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//for(i=0;i<=5;i++){
mGridView.setAdapter(new MyImageAdapter(getApplicationContext()));
Log.i("counter", ""+counter);
//}
// TODO Auto-generated method stub
//mGridView.setAdapter(new MyImageAdapter(getApplicationContext()));
//Toast.makeText(MyGridView.this, "No Images", Toast.LENGTH_SHORT).show();
}
}
);
}
}
class MyImageAdapter extends BaseAdapter{
private Context mContext;
ImageView imageview;
int counter;
public MyImageAdapter(Context c){
mContext = c;
}
public MyImageAdapter(OnClickListener onClickListener) {
// TODO Auto-generated constructor stub
}
#Override
public int getCount() {
// TODO Auto-for(i = 6;i<=counter;i++) {generated method stub
//counter = mThumbsIds.length;
//System.out.println("Counter = "+counter);
return 5;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView == null){
imageview = new ImageView(mContext);
imageview.setLayoutParams(new GridView.LayoutParams(85,85));
imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageview.setPadding(8, 8, 8, 8);
}
else{
imageview = (ImageView) convertView;
}
//int[] mThumbIds = null;
imageview.setImageResource(mThumbsIds[position]);
return imageview;
//return null;
}
private Integer[] mThumbsIds = {
R.drawable.image1,R.drawable.image2,
R.drawable.image3,R.drawable.image4,
R.drawable.image5,R.drawable.icon,
R.drawable.icon,R.drawable.icon,
R.drawable.icon};
}
Please help me out to correct this.
To refresh grid view see this search result

Categories

Resources