2012-01-20 20 views
6

Desidero acquisire il contenuto di ImageView utilizzando DrawingCache. Ho scritto il codice seguente.getDrawingCache() restituisce sempre null

iv1 = (ImageView)findViewById(R.id.iv1); 
iv2 = (ImageView)findViewById(R.id.iv2);    
iv1.setDrawingCacheEnabled(true); 
Bitmap myScreenshot = iv1.getDrawingCache(); 
iv2.setImageBitmap(myScreenshot); 

Ma sto ottenendo solo un'immagine sullo schermo. Più tardi sono venuto a sapere myScreenshot è nullo

Ho visto molti post relativi allo stesso problema, ma nessuna soluzione adeguata.

Ho pensato a eventuali autorizzazioni che dobbiamo aggiungere in manifest? o il permesso di root richiesto per raggiungere questo? Per favore aiutami riguardo a questo problema.

risposta

10

Provare a richiamare buildDrawingCache() prima getDrawingCache()

EDIT: chiamata getDrawingCache(), dopo che la pagina è caricato, invece di onCreate

+0

Ho aggiunto iv1.buildDrawingCache(); prima di getDrawingCache(). non utilizzare –

+0

hai verificato che iv1 abbia un'immagine quando viene eseguita l'applicazione. Specifica anche dove stai chiamando esattamente il tuo codice. Potresti chiamarlo prima che le viste siano inizializzate – aqs

+0

nel file xml, solo che sto dando ad android: src per iv1. Il codice che ho postato qui è dopo setContentView (R.layout.main); in onCreate() È corretto? –

0
imageview.setBackgroundResource(R.drawable.imageview);     

imageview1.setBackgroundResource(R.drawable.imageview1); 

utilizzare questa due immagini è screan

<RelativeLayout 
    android:id="@+id/item"    
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <ImageView 
     android:id="@+id/image" 
     android:layout_width="320dp" 
     android:layout_height="486dp" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentRight="true" 
     android:background="@drawable/iv1" /> 

    <ImageView 
     android:id="@+id/ic_launcer" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:background="@drawable/iv2" /> 

</RelativeLayout> 
1

Non puoi fare iv1.buildDrawingCache(true); aggiungi riga prima
Bitmap myScreenshot = iv1.getDrawingCache();

+0

La tua risposta è esattamente la stessa risposta accettata da @aqs – gecco

2

chiamata getDrawingCache() in onWindowFocusChanged() e non in onCreate(), quindi non sarà possibile ottenere nulla.

+0

grazie, ha funzionato per me! –

Problemi correlati