-1

これは、ユーザーがモバイルから画像を選択するのに役立つアクティビティ ファイルです。次に、それを壁紙として設定しますが、画像を選択しても、モバイルの壁紙には何も起こりません。マニフェスト ファイルにもアクセス許可を作成しました。モバイルでウィンドウを開いて画像を選択するまでは機能しているようですが、その後は機能しているだけで壁紙は変更されていません。エラーは 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>
4

1 に答える 1

1

あなたのマニフェストは間違っています。次のようにします。

<?xml version="1.0" encoding="utf-8"?>

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.mca3project" >

<uses-permission android:name="android.permission.SET_WALLPAPER"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <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>

于 2015-11-09T18:50:33.873 に答える