Bugün kü yazımda android uygulaması olarak galeriden bir resim seçip , seçilen resmi telefon duvar kağıdı yapan basit bir uygulamanın kodlarını paylaşacağım.
öncelikle manifest dosyasına gerekli izinleri ekleyelim.
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
Kodlar :
package
com.example.lenovo.setwallpaper;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import java.io.IOException;
public class MainActivity extends Activity {
private static final int IMAGE_PICK = 1;
//declerations
private ImageView imageView; // show choosen image
private Button buttonImage; //choose image
private Button buttonsetwallpaper; //set wallpaper
private String image_dir; //image directory
private WallpaperManager myWallpaperManager;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//declerations
this.myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
this.imageView = (ImageView) this.findViewById(R.id.imageView2);
this.buttonImage = (Button) this.findViewById(R.id.button_from_phone);
this.buttonsetwallpaper=(Button) this.findViewById(R.id.button_setwal);
//image from gallery
this.buttonImage.setOnClickListener(new ImagePickListener());
//button set wallpaper
this.buttonsetwallpaper.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
try {
myWallpaperManager.setBitmap(BitmapFactory.decodeFile(image_dir));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
//image from gallery
class ImagePickListener implements OnClickListener
{
public void onClick(View v)
{
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "choose a image"), IMAGE_PICK);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case IMAGE_PICK:
this.imageFromGallery(resultCode, data);
break;
default:
break;
}
}
}
private void imageFromGallery(int resultCode, Intent data)
{
Uri selectedImage = data.getData();
String [] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
this.image_dir=filePath; // image directory
this.imageView.setImageBitmap(BitmapFactory.decodeFile(filePath));
cursor.close();
}
}
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import java.io.IOException;
public class MainActivity extends Activity {
private static final int IMAGE_PICK = 1;
//declerations
private ImageView imageView; // show choosen image
private Button buttonImage; //choose image
private Button buttonsetwallpaper; //set wallpaper
private String image_dir; //image directory
private WallpaperManager myWallpaperManager;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//declerations
this.myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
this.imageView = (ImageView) this.findViewById(R.id.imageView2);
this.buttonImage = (Button) this.findViewById(R.id.button_from_phone);
this.buttonsetwallpaper=(Button) this.findViewById(R.id.button_setwal);
//image from gallery
this.buttonImage.setOnClickListener(new ImagePickListener());
//button set wallpaper
this.buttonsetwallpaper.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
try {
myWallpaperManager.setBitmap(BitmapFactory.decodeFile(image_dir));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
//image from gallery
class ImagePickListener implements OnClickListener
{
public void onClick(View v)
{
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "choose a image"), IMAGE_PICK);
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case IMAGE_PICK:
this.imageFromGallery(resultCode, data);
break;
default:
break;
}
}
}
private void imageFromGallery(int resultCode, Intent data)
{
Uri selectedImage = data.getData();
String [] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
this.image_dir=filePath; // image directory
this.imageView.setImageBitmap(BitmapFactory.decodeFile(filePath));
cursor.close();
}
}
Ayrıca arkadaşlar bu proje ve başka proje kodlarına githubta https://github.com/bhtyrsm
adresinden bulabilirsiniz.
adresinden bulabilirsiniz.
umarım faydası olur. Bir sorunuz olur ise benimle iletişime geçebilirsiniz.Kolay gelsin