0

Mon application affiche une image dans une vue d'image. Je souhaite que les utilisateurs sélectionnent l'image définie en option à partir de l'application elle-même. Comme ceci Set as optionComment ajouter "Définir comme" ou "Définir l'image comme" Option dans mon application Android ImageView

Comment puis-je ajouter le menu contextuel "Définir comme" afin que les utilisateurs puissent directement définir l'image comme fond d'écran? L'image est déjà stockée dans le dossier Drawable.

+0

Copie possible de [l'image de l'ensemble Android comme icône de contact/fond d'écran] (http://stackoverflow.com/questions/7284142/android-set-image-as-contact-icon-wallpaper) – Ads

+0

J'ai essayé de suivre leurs méthodes mais maintenant Je suis coincé [ici] (http://stackoverflow.com/questions/35619631/unable-to-set-picture-as-no-appps-can-perform-this-action) –

Répondre

0

Vous pouvez utiliser le code suivant pour "définir l'image sous". Ici, je prends la capture d'écran de l'activité en cours. Ensuite, je stocke cette activité sous forme de bitmap. Après que je fournirai cette activité à l'intention de "Définir comme option"

Sur CLICK:

OnButtonClick(){ 
ImageProcessing imageProcessing = new ImageProcessing(); 
      Bitmap bitmap = imageProcessing.takeScreenshot(getWindow().getDecorView().findViewById(R.id.view_thought)); 
      imageProcessing.saveBitmap(bitmap); 
      Intent intent = imageProcessing.setAsOption(this,imageProcessing.getSavedImagePath()); 
      startActivityForResult(Intent.createChooser(intent, "Set image as"), 200); 
} 

Mettre en oeuvre une nouvelle classe ImageProcessing

public class ImageProcessing { 
private File imagesPath; 
public void saveBitmap(Bitmap bitmap) { 
    imagesPath = new File(Environment.getExternalStorageDirectory() + "/screenshot.jpg"); 
    FileOutputStream fos; 
    try { 
     fos = new FileOutputStream(imagesPath); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
     fos.flush(); 
     fos.close(); 
    } catch (FileNotFoundException e) { 
     Log.e("POS", e.getMessage(), e); 
    } catch (IOException e) { 
     Log.e("POS", e.getMessage(), e); 
    } 
} 
public File getSavedImagePath(){ 
    imagesPath = new File(Environment.getExternalStorageDirectory() + "/screenshot.jpg"); 
    return imagesPath; 
} 

public Bitmap takeScreenshot(View rootView) { 
    rootView.setDrawingCacheEnabled(true); 
    return rootView.getDrawingCache(); 
} 
public Intent setAsOption(Context cntxt,File imagesPath){ 
    /*File imagesPath = new File(Environment.getExternalStorageDirectory() + "/screenshot.jpg");*/ 
    Intent intent = new Intent(Intent.ACTION_ATTACH_DATA); 
    if(imagesPath.exists()){ 
     Uri contentUri = FileProvider.getUriForFile(cntxt, BuildConfig.APPLICATION_ID+".Utility.GenericFileProvider",imagesPath); 

     intent.setDataAndType(contentUri, "image/jpg"); 
     intent.putExtra("mimeType", "image/jpg"); 
     intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
    }else { 
     Toast.makeText(cntxt,"Not a wallpaper",Toast.LENGTH_SHORT).show(); 
    } 
    return intent; 
} 

}

Dans menifest add :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
<uses-permission android:name="android.permission.SET_WALLPAPER" />