androidimageviewrootview

How to take screenshot of an imageView?


This is how do i get root view:

View rootView = findViewById(android.R.id.content).getRootView();

It works for me to take screenshot of rootview. But my question is how to define only an imageView for this View? Then only i can take screenshoot of specific image view.

Note: View rootView = findViewById(android.R.id.**imageview**).getRootView(); cause error.

UPDATE: Class and xml files which can take screen shoot for a specific imageView. Updated after solved!

Activity class:

public class SStest extends Activity {

Button button1;
LinearLayout ll2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sstest);
    
    Button button1= (Button) findViewById(R.id.button1);
    ll2 = (LinearLayout )findViewById(R.id.ll2);
    
    button1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
        // TODO Auto-generated method stub

                    Bitmap bitmap = takeScreenshot();
        saveBitmap(bitmap);
        
        }
    });


}
public Bitmap takeScreenshot() {

    View rootView=getWindow().getDecorView().findViewById(R.id.ll2);
rootView.setDrawingCacheEnabled(true);     
return rootView.getDrawingCache();
       
    }

public void saveBitmap(Bitmap bitmap) {
    
String root = Environment.getExternalStorageDirectory().toString();
    File newDir = new File(root + "/MapCards");    
    newDir.mkdirs();
    Random gen = new Random();
    int n = 10000;
    n = gen.nextInt(n);
    String fotoname = "Photo-"+ n +".jpg";
    File file = new File (newDir, fotoname);
    if (file.exists ()) file.delete (); 
    try {
        FileOutputStream fos = new FileOutputStream(file);
        bitmap.compress(CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}
}

.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/stainbck"
android:orientation="vertical" >

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Button" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:text="TextView"
    android:textSize="15dp" />

<LinearLayout
    android:id="@+id/ll2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"           
        android:background="@drawable/ic_launcher" />
</LinearLayout>

Solution

  • Here you need to change this from

    View rootView = findViewById(android.R.id.content).getRootView();
    

    to

    View rootView = findViewById(R.id.imageview).getRootView();
    

    Another thing you can do as follow.

    ImageView iv;
    

    Now find ID for it on onCrate() method

    iv = (ImageView)findViewById(R.id.imageview);
    

    Now in your Method

    View rootView = iv.getRootView();
    

    UPDATE:

    Change your xml file like,

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/ll1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#ff0000"
        android:orientation="vertical" >
    
        <Button
            android:id="@+id/button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Button" />
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:text="TextView"
            android:textSize="15dp" />
    
        <LinearLayout
            android:id="@+id/rootView"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
    
            <ImageView
                android:id="@+id/imageview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="15dp"
                android:background="@drawable/ic_launcher" />
        </LinearLayout>
    
    </LinearLayout>
    

    Now no need to find ID for ImageView instead of find ID for LinearLayout.

    LinearLayout ll;
    

    Now on onCreate()

    ll = (LinearLayout )findViewById(R.id.rootView);
    

    Now in your method

    View rootView = ll.getRootView();