2015-11-09 1 views
-1

Il s'agit d'un fichier d'activité qui aide l'utilisateur à choisir une image à partir d'un mobile. Puis le définir comme fond d'écran mais quand je choisis une image rien ne se passe au fond d'écran de mon mobile. J'ai également créé une autorisation dans le fichier manifeste. Il semble fonctionner jusqu'à ce que j'ouvre la fenêtre dans un mobile pour choisir une image, mais après cela, il fonctionne, mais ne change pas le fond d'écran. L'erreur doit être dans la méthode onActivityResult.Activité qui sélectionne une image à partir d'un mobile et la définit comme fond d'écran via un bouton.

Code: package 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(); 
       } 
      } 


     } 
    } 

erreur Logcat après avoir fait défiler vers le haut avec beaucoup Aucun filtre i trouvé ces: je peux voir l'autorisation de SETWALLPAPER est d'avoir une sorte de problème.

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)  

Ceci est mon fichier manifeste:

<?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> 
+0

Quelles sont les autorisations que vous avez ajoutés? –

+0

J'ai ajouté cette autorisation: Appy

+0

Est-ce qu'il attrape une erreur, ou est-ce qu'il ne fonctionne pas visuellement? –

Répondre

1

Votre manifeste est erroné. Assurez-vous comme ceci:

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