I am working on a project in which click on an image from gridview opens that image in an ImageView , i got this code from this website http://www.androidhive.info/2012/02/android-gridview-layout-tutorial/ , so i modified the FullImageActivity.java and added a button to set the image as background, but everytime i click on the button, it just force closes my app, P.S i have added the permission in the manifest also
here is my code
package com.example.androidhive;
import java.io.IOException;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class FullImageActivity extends Activity {
private WallpaperManager imageView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// get intent data
Intent i = getIntent();
// Selected image id
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]);
}
public void setaswall(View view) { // SET AS WALLPAPER BUTTON
// TODO Auto-generated method stub
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
Bitmap bitmap=((BitmapDrawable)imageView.getDrawable()).getBitmap();
if(bitmap!=null)
myWallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
I think you may also be trying to use it like this:
private ImageView mImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
mImageView = (ImageView) findViewById(R.id.full_image_view);
mImageView.setImageBitmap(BitmapFactory.decodeResource(getResources(), imageAdapter.mThumbIds[position]));
}
public void setaswall(View view) {
WallpaperManager wm = WallpaperManager.getInstance(this);
try {
final Bitmap bitmap = ((BitmapDrawable) mImageView.getDrawable()).getBitmap();
if (bitmap != null) {
wm.setBitmap(bitmap);
}
} catch (final IOException e) {
e.printStackTrace();
}
}
And make sure you've included the set wallpaper permission in your AndroidManifest.
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
Here you declare a field with the name "imageView", but of type WallpaperManager:
private WallpaperManager imageView;
then later you declare some local imageView:
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
last not least you are trying to get a Drawable from the field imageView that you have never initialized:
Bitmap bitmap=((BitmapDrawable)imageView.getDrawable()).getBitmap();
This will throw an exception which you later catch and print the stacktrace
==> Solution: initialize the field imageView properly and don't create local var that hides the field.
It is simple I think you may forgot add the Permission in your Mainfest file you
just add this in your Mainfest file
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
Related
Would anyone tell me how to pass images from one activity to another activity using extras (the idea is). I am displaying a set of images in activity1 as a horizontal scroll view. When I click on an image it should be displayed in another activity (activity2) with totally different layout.
I'll be satisfied if the explanation is done with json as well I have tried this (http://www.vogella.com/tutorials/AndroidIntent/article.html) tutorial but can't really find the answer.
MAIN ACTIVITY:
package com.example.user.horizontal;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class MainActivity extends Activity implements OnClickListener {
ImageView Display;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView im1 = (ImageView) findViewById(R.id.image1);
ImageView im2 = (ImageView) findViewById(R.id.image2);
ImageView im3 = (ImageView) findViewById(R.id.image3);
ImageView im4 = (ImageView) findViewById(R.id.image4);
ImageView im5 = (ImageView) findViewById(R.id.image5);
ImageView im6 = (ImageView) findViewById(R.id.image6);
ImageView im7 = (ImageView) findViewById(R.id.image7);
im1.setOnClickListener(this);
im2.setOnClickListener(this);
im3.setOnClickListener(this);
im4.setOnClickListener(this);
im5.setOnClickListener(this);
im6.setOnClickListener(this);
im7.setOnClickListener(this);
}
public void onClick(View v) {
Intent clickimage = new Intent(this, OnClick.class);
switch (v.getId()) {
case R.id.image1:
Display.setImageResource(R.drawable.images1);
clickimage.putExtra("display1", "images1");
startActivity(clickimage);
break;
case R.id.image2:
Display.setImageResource(R.drawable.images2);
clickimage.putExtra("display2", "pic2filename");
startActivity(clickimage);
break;
case R.id.image3:
Display.setImageResource(R.drawable.images8);
clickimage.putExtra("display3", "pic3filename");
startActivity(clickimage);
break;
case R.id.image4:
Display.setImageResource(R.drawable.images4);
clickimage.putExtra("display4", "pic4filename");
startActivity(clickimage);
break;
case R.id.image5:
Display.setImageResource(R.drawable.images5);
clickimage.putExtra("display5", "pic5filename");
startActivity(clickimage);
break;
case R.id.image6:
Display.setImageResource(R.drawable.images6);
clickimage.putExtra("display6", "pic6filename");
startActivity(clickimage);
break;
case R.id.image7:
Display.setImageResource(R.drawable.images8);
clickimage.putExtra("display7", "pic7filename");
startActivity(clickimage);
break;
}
}
SECOND ACTIVITY :
package com.example.user.horizontal;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
public class OnClick extends Activity{
private Bitmap mImage1;
private Bitmap mImage2;
private Bitmap mImage3;
private Bitmap mImage4;
private Bitmap mImage5;
private Bitmap mImage6;
private Bitmap mImage7;
private Bitmap mImage8;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.onclick);
Bundle bundle = getIntent().getExtras();
String imageName = bundle.getString("display");
// click listeners
ImageView imageView = (ImageView)findViewById(R.id.image1);
mImage1 = BitmapFactory.decodeResource(getResources(), R.drawable.images1);
mImage2 = BitmapFactory.decodeResource(getResources(), R.drawable.images2);
mImage3 = BitmapFactory.decodeResource(getResources(), R.drawable.images4);
mImage4 = BitmapFactory.decodeResource(getResources(), R.drawable.images5);
mImage5 = BitmapFactory.decodeResource(getResources(), R.drawable.images6);
mImage6 = BitmapFactory.decodeResource(getResources(), R.drawable.images7);
mImage7 = BitmapFactory.decodeResource(getResources(), R.drawable.images8);
mImage8 = BitmapFactory.decodeResource(getResources(), R.drawable.images);
if(imageName.matches("images1")) {
String b = bundle.getString("b");
imageView.setImageBitmap(mImage1);
}
else if (imageName.matches("pic2filename")) {
imageView.setImageBitmap(mImage2);
}
else if (imageName.matches("pic3filename")) {
imageView.setImageBitmap(mImage3);
}
else if (imageName.matches("pic4filename")) {
imageView.setImageBitmap(mImage4);
}
else if (imageName.matches("pic2filename")) {
imageView.setImageBitmap(mImage5);
}
else if (imageName.matches("pic5filename")) {
imageView.setImageBitmap(mImage6);
}
else if (imageName.matches("pic6filename")) {
imageView.setImageBitmap(mImage7);
}
else if (imageName.matches("pic7filename")) {
imageView.setImageBitmap(mImage8);
}
}
}
I am new to Java and Android.
Try passing the image file path or image url to next activity using extras in android
Pass Bitmap using intent :
Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);
and retrieve it on the other end:
Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");
Don't pass images between activities.
You should pass the path to image in Intent and show it in your new Activity. This is a correct way to do it.
i'm getting an error on one of the lines of code that I can't seem to find the solution.
The error is on this line:
cameraButton.setOnClickListener(cameraListener);
The error im getting is "Syntax error on token(s)
MainActivity
package com.example.triptych4;
import java.io.File;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
// label our logs "CameraApp3"
private static String logtag = "CameraApp3";
// tells us which camera to take a picture from
private static int TAKE_PICTURE = 1;
// empty variable to hold our image Uri once we store it
private Uri imageUri;
private Integer[] pics = { R.drawable.android, R.drawable.android3d,
R.drawable.background3 };
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
//create adapter Gallery
gallery.setAdapter(new ImageAdapter(this));
imageView = (ImageView) findViewById(R.id.imageView1);
gallery.setOnItemClickListener(new onItemClickListener() {
#Oveerride
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
Toast.makeText(getApplicationContext(), "pic:" + arg2, Toast.LENGTH_SHORT).show();
imageView.setImageResource(pics[arg2]);
}
// look for the button we set in the view
ImageButton cameraButton = (ImageButton)
findViewById(R.id.button_camera);
// set a listener on the button
cameraButton.setOnClickListener(cameraListener);
}
// set a new listener
private OnClickListener cameraListener = new OnClickListener() {
public void onClick(View v) {
// open the camera and pass in the current view
takePhoto(v);
}
};
public void takePhoto(View v) {
// tell the phone we want to use the camera
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
// create a new temp file called pic.jpg in the "pictures" storage area of the phone
File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "pic.jpg");
// take the return data and store it in the temp file "pic.jpg"
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
// stor the temp photo uri so we can find it later
imageUri = Uri.fromFile(photo);
// start the camera
startActivityForResult(intent, TAKE_PICTURE);
}
#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{
private Context context;
int imageBackground;
public ImageAdapter(Context context){
this.context = context;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return pics.length;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView imageView =new ImageView(context);
imageView.setImageResource(pics[arg0]);
return imageView;
}
}
// override the original activity result function
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// call the parent
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
// if the requestCode was equal to our camera code (1) then...
case 1:
// if the user took a photo and selected the photo to use
if(resultCode == Activity.RESULT_OK) {
// get the image uri from earlier
Uri selectedImage = imageUri;
// notify any apps of any changes we make
getContentResolver().notifyChange(selectedImage, null);
// get the imageView we set in our view earlier
ImageButton imageButton = (ImageButton)findViewById(R.id.button_camera);
// create a content resolver object which will allow us to access the image file at the uri above
ContentResolver cr = getContentResolver();
// create an empty bitmap object
Bitmap bitmap;
try {
// get the bitmap from the image uri using the content resolver api to get the image
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);
// set the bitmap to the image view
imageButton.setImageBitmap(bitmap);
// notify the user
Toast.makeText(MainActivity.this, selectedImage.toString(), Toast.LENGTH_LONG).show();
} catch(Exception e) {
// notify the user
Toast.makeText(MainActivity.this, "failed to load", Toast.LENGTH_LONG).show();
Log.e(logtag, e.toString());
}
}
}
}
}
You're missing some braces and semicolons, and you've got an #Oveerride in there as well.
Try this, it should compile:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
//create adapter Gallery
gallery.setAdapter(new ImageAdapter(this));
imageView = (ImageView) findViewById(R.id.imageView1);
gallery.setOnItemClickListener( new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int arg2, long arg3) {
Toast.makeText(getApplicationContext(), "pic:" + arg2, Toast.LENGTH_SHORT).show();
imageView.setImageResource(pics[arg2]);
}
});
// look for the button we set in the view
ImageButton cameraButton = (ImageButton)
findViewById(R.id.button_camera);
// set a listener on the button
cameraButton.setOnClickListener(cameraListener);
}
I would like to change an ImageView's image to black and white. The only thing is my code allows the user to take a photo, that photo is placed in the imageview. I would like that photo to be black and white. If anyone knows how I could do this, I would appreciate it.
MainActivity:
package com.example.triptych4;
import java.io.File;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
// label our logs "CameraApp3"
private static String logtag = "CameraApp3";
// tells us which camera to take a picture from
private static int TAKE_PICTURE = 1;
// empty variable to hold our image Uri once we store it
private Uri imageUri;
private Integer[] pics = { R.drawable.android, R.drawable.android3d,
R.drawable.background3 };
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Gallery gallery = (Gallery) findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
imageView = (ImageView) findViewById(R.id.imageView1);
gallery.setOnItemClickListener( new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getApplicationContext(), "pic:" + arg2,
Toast.LENGTH_SHORT).show();
imageView.setImageResource(pics[arg2]);
}
});
// look for the button we set in the view
ImageButton cameraButton = (ImageButton)
findViewById(R.id.button_camera);
// set a listener on the button
cameraButton.setOnClickListener(cameraListener);
}
// set a new listener
private OnClickListener cameraListener = new OnClickListener() {
public void onClick(View v) {
// open the camera and pass in the current view
takePhoto(v);
}
};
public void takePhoto(View v) {
// tell the phone we want to use the camera
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
// create a new temp file called pic.jpg in the "pictures" storage area of the phone
File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "pic.jpg");
// take the return data and store it in the temp file "pic.jpg"
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
// stor the temp photo uri so we can find it later
imageUri = Uri.fromFile(photo);
// start the camera
startActivityForResult(intent, TAKE_PICTURE);
}
#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{
private Context context;
int imageBackground;
public ImageAdapter(Context context){
this.context = context;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return pics.length;
}
#Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
#Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
ImageView imageView =new ImageView(context);
imageView.setImageResource(pics[arg0]);
return imageView;
}
}
// override the original activity result function
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// call the parent
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
// if the requestCode was equal to our camera code (1) then...
case 1:
// if the user took a photo and selected the photo to use
if(resultCode == Activity.RESULT_OK) {
// get the image uri from earlier
Uri selectedImage = imageUri;
// notify any apps of any changes we make
getContentResolver().notifyChange(selectedImage, null);
// get the imageView we set in our view earlier
ImageButton imageButton = (ImageButton)findViewById(R.id.button_camera);
// create a content resolver object which will allow us to access the image file at the uri above
ContentResolver cr = getContentResolver();
// create an empty bitmap object
Bitmap bitmap;
try {
// get the bitmap from the image uri using the content resolver api to get the image
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);
// set the bitmap to the image view
imageButton.setImageBitmap(bitmap);
// notify the user
Toast.makeText(MainActivity.this, selectedImage.toString(), Toast.LENGTH_LONG).show();
} catch(Exception e) {
// notify the user
Toast.makeText(MainActivity.this, "failed to load", Toast.LENGTH_LONG).show();
Log.e(logtag, e.toString());
}
}
}
}
}
Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.triptych5.MainActivity" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="200dp"
android:layout_height="200dp"
android:scaleType="fitXY"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/button_camera"/>
<ImageButton
android:id="#+id/button_camera"
android:layout_width="230dp"
android:layout_height="235dp"
android:scaleType="fitXY"
android:rotation="-90"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#drawable/middle4" />
<Gallery
android:id="#+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/imageView1"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
You can simply achieve this by doing:
ImageView imageview = (ImageView) findViewById(R.id.imageView1);
ColorMatrix matrix = new ColorMatrix();
matrix.setSaturation(0);
ColorMatrixColorFilter filter = new ColorMatrixColorFilter(matrix);
imageview.setColorFilter(filter);
This is the Kotlin Version
imageView.colorFilter = ColorMatrixColorFilter(ColorMatrix().apply { setSaturation(0f)})
You can use android.graphics.ColorFilter for your purpose.
You can use this sample to suite your need.
Pass the imageView to the setBW method like
setBW(imageView);
and the the functionality is
private void setBW(ImageView iv){
float brightness = 10; // change values to suite your need
float[] colorMatrix = {
0.33f, 0.33f, 0.33f, 0, brightness,
0.33f, 0.33f, 0.33f, 0, brightness,
0.33f, 0.33f, 0.33f, 0, brightness,
0, 0, 0, 1, 0
};
ColorFilter colorFilter = new ColorMatrixColorFilter(colorMatrix);
iv.setColorFilter(colorFilter);
}
Try using this. Any concerns. Let me know. Thanks.
I would also suggest using Kotlin extensions:
private const val MAX_SATURATION = 1f
private const val MIN_SATURATION = 0f
fun ImageView.setMaxSaturation() {
val matrix = ColorMatrix()
matrix.setSaturation(MAX_SATURATION)
colorFilter = ColorMatrixColorFilter(matrix)
}
fun ImageView.setMinSaturation() {
val matrix = ColorMatrix()
matrix.setSaturation(MIN_SATURATION)
colorFilter = ColorMatrixColorFilter(matrix)
}
Java version in one line
imageview.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix(){{setSaturation(0f);}}));
Right I've looked at a shed load of tutorials, questions and so on but none have worked, so I don't need more links, I just need a fix for my current code..
So far I have this:
public class FullscreenActivity extends Activity {
protected int[] mThumbIds;
#SuppressWarnings("unused")
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fullscreen);
// get intent data
Intent i = getIntent();
// Selected image id
final int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
Button SetWallpaper = (Button)findViewById(R.id.setwallpaper);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
}
private Object[] imageIDs;
private int position;
public void onClick(View arg0) {
try {
WallpaperManager.getInstance(this).setResource((Integer) imageIDs[position]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
But when I click the button once the picture has been selected, it doesn't do anything?
I also have the permission in the manifest but it still doesn't work, so any help would be hugely appreciated
Thank you
I'm not sure if this has anything to do with it or not but I'm testing the app on a Samsung Galaxy S4
May be it can relate..
package com.Engr.android;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
public class Camera extends Activity implements View.OnClickListener
{
ImageButton ib;
ImageView iv;
Button btn;
Intent i;
final static int cameraData = 0;
Bitmap bmp;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.photo);
Buttons();
InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
bmp = BitmapFactory.decodeStream(is);
}
private void Buttons()
{
ib = (ImageButton) findViewById(R.id.ibtnTakePic);
iv = (ImageView) findViewById(R.id.ivReturnPic);
btn = (Button) findViewById(R.id.btnSetWall);
btn.setOnClickListener(this);
ib.setOnClickListener(this);
}
#SuppressWarnings("deprecation")
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btnSetWall:
try {
getApplicationContext().setWallpaper(bmp);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.ibtnTakePic:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, cameraData);
break;
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK)
{
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
iv.setImageBitmap(bmp);
}
}
}
i have an imageview and button in the xml file .
loading the image1 and play song1.mp3 file.when the song is over it should load
next image and play the song2.mp3 song it should go on until the last image.
button for closing and exit the application.
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.widget.ImageView;
import android.app.Activity;
import android.content.res.AssetFileDescriptor;
public class MainActivity extends Activity {
public MediaPlayer mpp;
final int image[] = {R.drawable.apple,R.drawable.ball,R.drawable.cat};
int i=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView img = (ImageView) findViewById(R.id.img);
String audio[]={"song1.mp3","song2.mp3","song3.mp3"};
// MediaPlayer mp = new MediaPlayer();
AssetFileDescriptor descriptor;
try {
descriptor = getAssets().openFd(audio[i]);
mpp.setDataSource( descriptor.getFileDescriptor(), descriptor.getStartOffset(),descriptor.getLength());
descriptor.close();
mpp.prepare();
mpp.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mpp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer arg0) {
// TODO Auto-generated method stub
img.setImageResource(image[i]);
i++;
mpp.start();
}
});
}
}
Hard to say anything without LogCat output but it looks like you have null reference. Before calling img.setImageResource(image[i]) you have to assign ImageView to img variable, if that View is defined in your activity_main.xml you should add following line in onCreate():
img = (ImageView) findViewById(R.id.you_imageview_id);
For insufficient memory in test phone (helpful for someone who use the personal phone for development like me)
if the memory is really full then un install some unwanted applications.
If above statement is not true then restart the phone will solve the problem.
I got this error in my Samsung captivate phone. Restart the phone worked all times.