我正在尝试将位图图像设置为墙纸。我能够在ImageView中设置位图。但是,当我尝试打开将其设置为壁纸的意图应用程序崩溃。我已在下面发布了LogCat。
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.setType("image/*");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
Bitmap bitmap = wallpaper;
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(getContentResolver(), bitmap , "Title", "Description");
if(path != null)
intent.setData(Uri.parse(path));
startActivity(Intent.createChooser(intent, "Choose Application"));
但是我收到了这个logcat错误。
12-21 12:13:01.802: E/MediaStore(7761): Failed to insert image
12-21 12:13:01.802: E/MediaStore(7761): java.io.FileNotFoundException: No such file or directory
12-21 12:13:01.802: E/MediaStore(7761): at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:146)
12-21 12:13:01.802: E/MediaStore(7761): at android.content.ContentProviderProxy.openAssetFile(ContentProviderNative.java:577)
12-21 12:13:01.802: E/MediaStore(7761): at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:673)
12-21 12:13:01.802: E/MediaStore(7761): at android.content.ContentResolver.openOutputStream(ContentResolver.java:537)
12-21 12:13:01.802: E/MediaStore(7761): at android.content.ContentResolver.openOutputStream(ContentResolver.java:513)
12-21 12:13:01.802: E/MediaStore(7761): at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:891)
12-21 12:13:01.802: E/MediaStore(7761): at com.shyam.flickrwallpaperapp.Wallpaper.onClick(Wallpaper.java:164)
12-21 12:13:01.802: E/MediaStore(7761): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 12:13:01.802: E/MediaStore(7761): at java.lang.reflect.Method.invoke(Method.java:525)
12-21 12:13:01.802: E/MediaStore(7761): at android.view.View$1.onClick(View.java:3628)
12-21 12:13:01.802: E/MediaStore(7761): at android.view.View.performClick(View.java:4240)
12-21 12:13:01.802: E/MediaStore(7761): at android.view.View$PerformClick.run(View.java:17721)
12-21 12:13:01.802: E/MediaStore(7761): at android.os.Handler.handleCallback(Handler.java:730)
12-21 12:13:01.802: E/MediaStore(7761): at android.os.Handler.dispatchMessage(Handler.java:92)
12-21 12:13:01.802: E/MediaStore(7761): at android.os.Looper.loop(Looper.java:137)
12-21 12:13:01.802: E/MediaStore(7761): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-21 12:13:01.802: E/MediaStore(7761): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 12:13:01.802: E/MediaStore(7761): at java.lang.reflect.Method.invoke(Method.java:525)
12-21 12:13:01.802: E/MediaStore(7761): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-21 12:13:01.802: E/MediaStore(7761): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-21 12:13:01.802: E/MediaStore(7761): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
我相信你已经在下面更新了清单
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
我正在复制我的代码,这是Camera.java供您参考
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实现View.OnClickListener { ImageView iVReturnedPic; 按钮btnSetWall; ImageButton iBTakePic; static final int CameraData = 0; 位图bmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.photo);
initializePhotoVar();
InputStream ipstr = getResources().openRawResource(R.drawable.ic_launcher) ;
bmap = BitmapFactory.decodeStream(ipstr);
}
private void initializePhotoVar() {
iVReturnedPic = (ImageView) findViewById(R.id.iV_ReturnedPic);
iBTakePic = (ImageButton) findViewById(R.id.iB_TakePic);
btnSetWall = (Button) findViewById(R.id.btn_SetWall);
btnSetWall.setOnClickListener(this);
iBTakePic.setOnClickListener(this);
}
@SuppressWarnings("deprecation")
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_SetWall:
try {
getApplicationContext().setWallpaper(bmap);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case R.id.iB_TakePic:
Intent CamIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(CamIntent, 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 = (Bundle) data.getExtras();
bmap = (Bitmap) extras.get("data");
iVReturnedPic.setImageBitmap(bmap);
}
}
}