我尝试创建一个简单的Android应用程序,让你拍照并将其设置为设备壁纸。
我还插入了调整位图大小以匹配屏幕大小的代码(崩溃可能是因为这个)。
但是当我按下“设置壁纸”按钮时,应用程序崩溃了。你能帮我弄清楚出了什么问题吗?
这是JAVA:
package com.example.androidwall;
import java.io.IOException;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Matrix;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity {
public static final int REQUEST_IMAGE_CAPTURE = 1;
Bitmap bmp;
WallpaperManager myWallpaperManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void takePictureIntentStart(View view){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
}
}
public void setWallpaper(View view){
int screenWidth = myWallpaperManager.getDesiredMinimumWidth();
int screenHeight = myWallpaperManager.getDesiredMinimumHeight();
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bmp, screenWidth, screenHeight, true);
if(resizedBitmap != null){
myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setBitmap(resizedBitmap);
} catch (IOException e) {
Toast.makeText(this, "Error setting wallpaper", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
else if(bmp == null){
Toast.makeText(this, "Please try again after taking the picture", Toast.LENGTH_LONG).show();
}
}
}
这是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"
tools:context="${relativePackage}.${activityClass}" >
<Button
android:id="@+id/takePicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:text="@string/takePicture"
android:onClick="takePictureIntentStart" />
<Button
android:id="@+id/setWallpaper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/takePicture"
android:layout_centerHorizontal="true"
android:layout_marginTop="45dp"
android:onClick="setWallpaper"
android:text="@string/setWallpaper" />
</RelativeLayout>
请帮帮我。
答案 0 :(得分:1)
您的堆栈跟踪已丢失,但我相信您有一个空指针异常。原因是,您正在使用:
WallpaperManager myWallpaperManager;
在它被初始化之前。见这里:
public void setWallpaper(View view){
int screenWidth = myWallpaperManager.getDesiredMinimumWidth();
int screenHeight = myWallpaperManager.getDesiredMinimumHeight();
此时您的myWallpaperManager
仍为null
。在方法开始后立即移动您的单例请求myWallpaperManager = WallpaperManager.getInstance(getApplicationContext());
,例如:
public void setWallpaper(View view){
myWallpaperManager = WallpaperManager.getInstance(getApplicationContext()); // <--- This line of code could be here or you could another object creation.
int screenWidth = myWallpaperManager.getDesiredMinimumWidth();
int screenHeight = myWallpaperManager.getDesiredMinimumHeight();
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bmp, screenWidth, screenHeight, true);
if(resizedBitmap != null){
try {
myWallpaperManager.setBitmap(resizedBitmap);
} catch (IOException e) {
Toast.makeText(this, "Error setting wallpaper", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
else if(bmp == null){
Toast.makeText(this, "Please try again after taking the picture", Toast.LENGTH_LONG).show();
}
}
答案 1 :(得分:1)
如果你的内存不足,那么
myWallpaperManager.setImageBitmap(decodeSampledBitmapFromResource( mytext.getName(), screenWidth,screenHeight));
private Bitmap decodeSampledBitmapFromResource(String filePath, int i, int j) {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, i, j);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(filePath, options);
}
private int calculateInSampleSize(BitmapFactory.Options options, int j, int k) {
// TODO Auto-generated method stub
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > j || width > k) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > j
&& (halfWidth / inSampleSize) > k) {
inSampleSize *= 2;
}
}
return inSampleSize;
}