I would like the user to get the option once the chosen image is clicked to set the image as their wallpaper or to save it to their SD card.
This is my first time doing this so I need some guidance. I have looked at other questions similar to this one but everyone uses different methods to the one I have done to set up displaying the images.
Thanks in advance, here's the code:
AdapterView for Displayimagesin:
package com.question;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class AdapterViewADV extends BaseAdapter {
private Context mContext;
public Integer[] mThumbIds = {
R.drawable.Image1,
R.drawable.Image2,
R.drawable.Image3,
R.drawable.Image4,
R.drawable.Image5,
R.drawable.Image6
};
public AdapterViewADV(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 = null;
if(convertView == null){
imageView = new ImageView(mContext);imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(NO_SELECTION, NO_SELECTION));
convertView = imageView;
}else{
imageView = (ImageView)convertView;
}
imageView.setImageResource(mThumbIds[position]);
return convertView;
}
}
Class displaying images in:
package com.question;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
public class Displayimagesin extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_battlefield4);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new AdapterViewADV(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(Displayimagesin.this, "Wallpaper set",
Toast.LENGTH_SHORT).show();
}
});
}
}
set wallpaper by calling WallpaperManager .
get a reference to your image to Bitmap.
something like this
WallpaperManager wm=WallpaperManager.getInstance(this);
wm.setBitmap(bitmap);
and in manifest file add permission
android.permission.SET_WALLPAPER
hope it helps.
Try to use WallpaperManager to set wallpaper
WallpaperManager myWallpaperManager=WallpaperManager.getInstance(getApplicationContext());
myWallpaperManager.setResource(mThumbIds[curruntPosition]);
Related
I'm newbie and just study Java around 1 month.
i'm trying to create group picture by grid view and if i click any picture then show for user alert dialog ask user want to delete picture or not. But somehow when i try to set event for grid view by image. i can't start up app. can help me find where is crash cause app can't start up.
package com.example.hi.gridviewhinhanh;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ArrayList<Danhsachhinhanh> manghinhanh;
HinhanhAdapter arrayAdapter;
GridView gridView;
ImageView imgView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridView = findViewById(R.id.gridview);
manghinhanh = new ArrayList<>();
manghinhanh.add(new Danhsachhinhanh(R.drawable.chupanh));
manghinhanh.add(new Danhsachhinhanh(R.drawable.goidien));
manghinhanh.add(new Danhsachhinhanh(R.drawable.photoshop));
manghinhanh.add(new Danhsachhinhanh(R.drawable.quality));
manghinhanh.add(new Danhsachhinhanh(R.drawable.secutiry));
manghinhanh.add(new Danhsachhinhanh(R.drawable.signuptoday));
arrayAdapter = new HinhanhAdapter(MainActivity.this, manghinhanh);
gridView.setAdapter(arrayAdapter);
imgView = findViewById(R.id.imageviewHinhanh);
imgView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder hopthoai = new AlertDialog.Builder(MainActivity.this);
hopthoai.setTitle("Do you want to delete");
hopthoai.setMessage("Please confirm");
hopthoai.setIcon(R.mipmap.ic_launcher);
hopthoai.setCancelable(false);
hopthoai.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
hopthoai.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "You choose No", Toast.LENGTH_SHORT).show();
}
});
}
});
}
}
package com.example.hi.gridviewhinhanh;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;
public class HinhanhAdapter extends BaseAdapter {
Context context;
ArrayList<Danhsachhinhanh> danhsachhinhanhArrayList;
public HinhanhAdapter (Context context, ArrayList<Danhsachhinhanh> danhsachhinhanhArrayList){
this.context = context;
this.danhsachhinhanhArrayList = danhsachhinhanhArrayList;
}
#Override
public int getCount() {
return danhsachhinhanhArrayList.size();
}
#Override
public Object getItem(int position) {
return danhsachhinhanhArrayList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(R.layout.dong_item_gridviewhinhanh,null);
ImageView imageView = convertView.findViewById(R.id.imageviewHinhanh);
Danhsachhinhanh danhsachhinhanh = (Danhsachhinhanh) getItem(position);
imageView.setImageResource(danhsachhinhanh.getHinhanh());
return convertView;
}
}
place you imageView clickListener in Adapter's getView() and check it again.
Becuase your imageView does not exist there. For more information read about adapter and recycleview.
I have had a basic Gridview sample from Google's documentation, which worked perfectly.
I also have some code that provides result as ArrayList with Bitmaps of last created pictures in Android's external storage (Permissions in manifest are granted) and that code also worked just fine on other project.
When I merged that concepts into one simple app code isn't working at all, activity remains empty till app crashes. However I noticed something interesting: There's a for loop which has defined number of iterations by int capacity but in Logcat I can see that number of iterations in this loop surpassing declared int value, basically that's reason why app crashes (outOfBoundsException).
So i Have those 2 problems and sitting on that for couple of hours... here is my code:
MainActivity.java
package com.example.patryk.hellogridview;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Toast.makeText(MainActivity.this, "" + position,
Toast.LENGTH_SHORT).show();
}
});
}
PhotosReader.java
package com.example.patryk.hellogridview;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.MediaStore;
import android.util.Log;
import java.util.ArrayList;
public class PhotosReader {
private int capacity = 5;
private ArrayList<Bitmap> photos = new ArrayList<Bitmap>(capacity);
public int getCapacity() {
return capacity;
}
public void setCapacity(int capacity) {
this.capacity = capacity;
}
public void setPhotos(ArrayList<Bitmap> photos) {
this.photos = photos;
}
public PhotosReader() {
}
public ArrayList<Bitmap> getPhotos(Context context) {
String[] projection = new String[]{
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA,
MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
MediaStore.Images.ImageColumns.DATE_TAKEN,
MediaStore.Images.ImageColumns.MIME_TYPE
};
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null,
null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
cursor.moveToFirst();
for(int i = 1; i<=capacity; i++){
photos.add(BitmapFactory.decodeFile(cursor.getString(1)));
cursor.moveToNext();
Log.d("photos.add", "iteration");
}
return photos;
}
public Bitmap getPhotosBitmap(ArrayList photos, int position){
Bitmap bm = (Bitmap) photos.get(position);
return bm;
}
}
ImageAdapter.java
package com.example.patryk.hellogridview;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return 0;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
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;
}
PhotosReader PR = new PhotosReader();
imageView.setImageBitmap(PR.getPhotosBitmap(PR.getPhotos(mContext), position));
return imageView;
}
}
This function inside ImageAdapter should not return 0. It should return the number of images that you want to show in the grid so should return capacity in your case. GridView assumes that there are 0 things to show
public int getCount() {
return 0;
}
change this to
public int getCount() {
return capacity;
}
Actually the correct way to do this would be to pass the PhotoReader object from the ImageAdapter constructor using ImageAdapter(context,photoReader) and then use photoReader.getCapacity() in getCount() function
I have a custom base image adapter in which i use for a grid view.. i was trying to use it for a full image view but i get an error while creating the my custom adapter object...passing the parameters....
By the way sorry for any mistakes ,this is my first question:
This is for gallery to preview full
Fullimage java:Where the error is present
public class FullImage extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_image);
Intent i= getIntent();
int position = i.getExtras().getInt("id");
MyPicsAdapter adapter = new MyPicsAdapter(this);//ERROR is here!!!
ImageView imageView = (ImageView) findViewById(R.id.fullimage);
imageView.setImageResource(adapter.item_image[position]);
}
}
Custom Adapter:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class MyPicsAdapter extends BaseAdapter{
public Context context;
public final int item_image[];
public final String item_text[];
public MyPicsAdapter(Context context, int item_image[], String[] item_text)
{
this.context = context;
this.item_image = item_image;
this.item_text = item_text;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = new View(context);
// get layout from custom_gridview.xml
gridView = inflater.inflate(R.layout.custom_gridview, null);
// set value into imageview
ImageView image = (ImageView) gridView.findViewById(R.id.item_image);
image.setImageResource(item_image[position]);
// set value into textview
TextView text = (TextView) gridView.findViewById(R.id.item_text);
text.setText(item_text[position]);
} else {
gridView = (View) convertView;
}
return gridView;
}
#Override
public int getCount() {
return item_text.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
Class where the images are:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
public class Smitpic extends AppCompatActivity {
GridView grid;
String text[] = {
"Colorful countryside on the way to Smit",
"A local bus at the Village square",
"Cattle grazing at the village ground",
"Pine trees lined the road leading to the Wooden house",
"Traditional wooden house made without any metal nails",
"Intricate carvings on the door of the wooden house",
"A cottage at Smit",
"Villagers returning home after washing clothes at the river",
"A river idlying flowing near Smit",
"Colorful field of grasses and pine trees"
};
int image[] = {R.drawable.smit1,R.drawable.smit2,
R.drawable.smit3,R.drawable.smit4,R.drawable.smit5,
R.drawable.smit6,R.drawable.smit7,R.drawable.smit8,
R.drawable.smit9,R.drawable.smit10
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_smitpic);
grid = (GridView)findViewById(R.id.simpleGrid);
grid.setAdapter(new MyPicsAdapter(this,image,text));
// grid.setAdapter(new ImageAdapter(this,image));
grid.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),text[position],Toast.LENGTH_LONG).show();
Intent a= new Intent(getApplicationContext(),FullImage.class);
a.putExtra("id",position);
startActivity(a);
}
});
}
}
You can pass only the image as follows,
grid.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),text[position],Toast.LENGTH_LONG).show();
Intent a = new Intent(getApplicationContext(),FullImage.class);
a.putExtra("id", image[position]);
startActivity(a);
}
});
Inside onCreate of FullImage Activity get the image and display it in the ImageView,
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_image);
int image = getIntent().getExtras().getInt("id");
ImageView imageView = (ImageView) findViewById(R.id.fullimage);
imageView.setImageResource(image);
}
I am new to Android and I want to create a MySQLite Database using a csv file which is on the sd card. Can you please help me with code.
package com.example.nrbapp;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
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.Toast;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent i=new Intent(getApplicationContext(),SecondActivity.class);
i.putExtra("position_id",""+position);
startActivity(i);
//Toast.makeText(MainActivity.this,position + "," + id, Toast.LENGTH_SHORT).show();
}
});
}
#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;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch(id) {
case R.id.action_settings:
return true;
case R.id.action_search:
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
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;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images
private Integer[] mThumbIds = {
R.drawable.bike, R.drawable.car,
R.drawable.muv, R.drawable.auto,
R.drawable.tractor, R.drawable.commercial
};
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
This is my MainActivity which is a image gridview and I need to create a database on click of any image.
Please help me with this
You can create SQLite database from CSV file using technique suggested in this post:
How to import load a .sql or .csv file into SQLite?
Then you can follow this article to use pre built sqlite database in your application.
http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/
I got a problem in gridview scroll. I have make one custom gridview and insert widget in raw file and inflate them through view. I got proper result and proper data and everything is gone well but when I scroll gridview 3-4 times up down speedy it raises an OutOfMemoryException.
This app contain list of installed app list and icon
Here is my custome adapter's code:
package com.AppFavorits;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.AppFavorits.ImageLoad.ImageLoader;
public class GridViewAdapter extends BaseAdapter {
private Context activit;
LayoutInflater inflator;
public RelativeLayout imgvGridItem;
public TextView txtGridItemlabel;
public CheckBox chkbxGridItem;
ArrayList<PInfo> lstpinfo = new ArrayList<PInfo>();
public ImageLoader imageLoader;
public GridViewAdapter(Context m1, ArrayList<PInfo> lstpinfo) {
activit = m1;
inflator = LayoutInflater.from(m1);
this.lstpinfo = lstpinfo;
imageLoader=new ImageLoader(m1.getApplicationContext());
}
public static class ViewHolder {
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder view;
// LayoutInflater inflator = activit.getLayoutInflater();
view = new ViewHolder();
convertView = inflator.inflate(R.layout.gridviewrow, null);
imgvGridItem = (RelativeLayout) convertView
.findViewById(R.id.rlGreidItemicon);
txtGridItemlabel = (TextView) convertView
.findViewById(R.id.txtGridItemlabel);
chkbxGridItem = (CheckBox) convertView
.findViewWithTag(R.id.chkbxGridItem);
if ((lstpinfo.get(position).appname.toString()) != null){
Drawable d = lstpinfo.get(position).icon;
//new ImageLoad().execute();
imgvGridItem.addView(getimageviewimage(lstpinfo.get(position).icon));
txtGridItemlabel.setText(lstpinfo.get(position).appname.toString());
// convertView.setTag(view);
}
return convertView;
}
#Override
public int getCount() {
return lstpinfo.size();
}
#Override
public Object getItem(int position) {
return lstpinfo.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
public RelativeLayout getimageviewimage(Drawable d) {
RelativeLayout seprator = new RelativeLayout(activit);
seprator.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
seprator.setGravity(Gravity.LEFT | Gravity.CENTER);
ImageView imgmap = new ImageView(activit);
imgmap.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
// imgmap.setBackgroundResource(R.drawable.a13);
Bitmap bitmap = ((BitmapDrawable)d).getBitmap();
imageLoader.DisplayImage(bitmap, imgmap);
//imgmap.setImageBitmap(bitmap);
seprator.setPadding(5, 5, 15, 5);
seprator.addView(imgmap);
return seprator;
}
private class ImageLoad extends AsyncTask<Void, Void, Void> {
// private final ProgressDialog dialog = new ProgressDialog(tranning.this);
protected void onPreExecute() {
// this.dialog.setMessage("Please Wait...");
// this.dialog.show();
// put your code which preload with processDialog
}
#Override
protected Void doInBackground(Void... arg0) {
// put your code here
return null;
}
#Override
protected void onPostExecute(final Void unused) {
/*if (this.dialog.isShowing()) {
this.dialog.dismiss();
} */
}
}
}
I think there are something wrong with getview.
In getView() you are not reusing convertView, but always inflating new one with inflator. This requires more memory and makes your code slower. Also, check that you are not leaking images with imageLoader.
out of memory usually means that there is not enough memory to load the file. try closing everything else (or atleast a few other apps) and trying again.
Download code From Following link and use gridview instead of listview
Lazy Loading Example