这是一个活动文件,可帮助用户从移动设备中选择图像。然后将其设置为墙纸,但是当我选择图像时,我的手机墙纸没有任何反应。我还在清单文件中创建了一个权限。它似乎一直在工作,直到我在手机中打开一个窗口来选择图像,但在那之后它只是工作但不改变壁纸。错误应该在 onActivityResult 方法中。
代码:包com.example.android.mca3project;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
public class Main2Activity extends AppCompatActivity {
private static final int PICK_IMAGE = 0;
InputStream inputStream;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
public void selectimagemethod(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK) {
if (data == null) {
//Display an error
return;
}
try {
inputStream = this.getContentResolver().openInputStream(data.getData());
} catch (FileNotFoundException e) {
Toast.makeText(getApplicationContext(), "Error! Image not found.", Toast.LENGTH_LONG).show();
}
Bitmap bm = BitmapFactory.decodeStream(inputStream);
//Now you can do whatever you want with your inpustream, save it as file, upload to a server, decode a bitmap...
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setBitmap(bm);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
在没有过滤器的情况下向上滚动很多之后出现 Logcat 错误我发现了这些:我可以看到 SETWALLPAPER 权限存在某种问题。
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: FATAL EXCEPTION: main
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: Process: com.example.android.mca3project, PID: 12150
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:61 flg=0x1 }} to activity {com.example.android.mca3project/com.example.android.mca3project.Main2Activity}: java.lang.SecurityException: Access denied to process: 12150, must have permission android.permission.SET_WALLPAPER
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at android.app.ActivityThread.deliverResults(ActivityThread.java:3588)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at android.app.ActivityThread.handleSendResult(ActivityThread.java:3631)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at android.app.ActivityThread.access$1300(ActivityThread.java:151)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5268)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: Caused by: java.lang.SecurityException: Access denied to process: 12150, must have permission android.permission.SET_WALLPAPER
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at android.os.Parcel.readException(Parcel.java:1546)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at android.os.Parcel.readException(Parcel.java:1499)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at android.app.IWallpaperManager$Stub$Proxy.setWallpaper(IWallpaperManager.java:218)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at android.app.WallpaperManager.setBitmap(WallpaperManager.java:755)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at com.example.android.mca3project.Main2Activity.onActivityResult(Main2Activity.java:57)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at android.app.Activity.dispatchActivityResult(Activity.java:6235)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at android.app.ActivityThread.deliverResults(ActivityThread.java:3584)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at android.app.ActivityThread.handleSendResult(ActivityThread.java:3631)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at android.app.ActivityThread.access$1300(ActivityThread.java:151)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5268)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:902)
11-09 23:44:13.653 12150-12150/com.example.android.mca3project E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:697)
这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.mca3project" >
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Main2Activity"
android:label="@string/title_activity_main2"
android:parentActivityName=".MainActivity"
android:theme="@style/AppTheme.NoActionBar" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.android.mca3project.MainActivity" />
</activity>
</application>
</manifest>