Take screensot and save android

前端 未结 2 1642
无人及你
无人及你 2020-12-17 04:43

I\'m a very new android developer with a basic understanding of java. I\'ve undertaken a large project and I\'ve hit a problem I can seem to fix.

Basically my proble

相关标签:
2条回答
  • 2020-12-17 05:14

    Hey You Can just use the below Code to capture screen shoot in android and save it in ur SDCard and then you can use it later

    // image naming and path  to include sd card  appending name you choose for file
    String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;   
    
    // create bitmap screen capture
    Bitmap bitmap;
    View v1 = mCurrentUrlMask.getRootView();
    v1.setDrawingCacheEnabled(true);
    bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);
    
    OutputStream fout = null;
    imageFile = new File(mPath);
    
    try {
        fout = new FileOutputStream(imageFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
        fout.flush();
        fout.close();
    
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    

    Then, when you need to access use something like this:

    Uri uri = Uri.fromFile(new File(mPath));
    
    0 讨论(0)
  • 2020-12-17 05:22
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    

    My layout

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/rl"
    tools:context=".MainActivity" >
    
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="70dp"
        android:text="Button" />
    
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="72dp"
        android:src="@drawable/afor" />
    
        </RelativeLayout>
    

    My Activity class

    public class MainActivity extends Activity {
    
    RelativeLayout rl; 
    final int PIC_CROP = 1;
    ImageView iv;
     File file;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        rl = (RelativeLayout) findViewById(R.id.rl);
         Button b= (Button) findViewById(R.id.button1); 
         iv= (ImageView) findViewById(R.id.imageView1);
         b.setOnClickListener(new OnClickListener()
         {
    
          @Override
          public void onClick(View v) {
              save();
         }
         });
    }
    
       public void save()
       {
           AlertDialog.Builder editalert = new AlertDialog.Builder(MainActivity.this);
           editalert.setTitle("Please Enter the name with which you want to Save");
           final EditText input = new EditText(MainActivity.this);
           LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
                   LinearLayout.LayoutParams.FILL_PARENT,
                   LinearLayout.LayoutParams.FILL_PARENT);
           input.setLayoutParams(lp);
           editalert.setView(input);
           editalert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int whichButton) {
                   rl.setDrawingCacheEnabled(true);
                   String name= input.getText().toString();
                   Bitmap bitmap =rl.getDrawingCache();
                   String root = Environment.getExternalStorageDirectory().toString();
                   File myDir = new File(root + "/MyDraw");    
                   myDir.mkdirs();
                   file = new File (myDir, name+".png");
                   if (file.exists ()) file.delete ();         
                   try 
                   {
                       if(!file.exists())
                   {
                       file.createNewFile();
                   }
                       FileOutputStream ostream = new FileOutputStream(file);
                       bitmap.compress(CompressFormat.PNG, 10, ostream);
                       ostream.close();
                       Uri uri = Uri.fromFile(new File(file.getAbsolutePath()));
                       performCrop(uri);                          
                   } 
                   catch (Exception e) 
                   {
                       e.printStackTrace();
                   }
               }
           });
           editalert.show(); 
       }
    
    
       private void performCrop(Uri picUri) {
        try {
    
       Intent cropIntent = new Intent("com.android.camera.action.CROP");
       // indicate image type and Uri
       cropIntent.setDataAndType(picUri, "image/*");
       // set crop properties
       cropIntent.putExtra("crop", "true");
       // indicate aspect of desired crop
       cropIntent.putExtra("aspectX", 1);
       cropIntent.putExtra("aspectY", 1);
       // indicate output X and Y
       cropIntent.putExtra("outputX", 128);
       cropIntent.putExtra("outputY", 128);
       // retrieve data on return
       cropIntent.putExtra("return-data", true);
       // start the activity - we handle returning in onActivityResult
       startActivityForResult(cropIntent, PIC_CROP);
     }
     // respond to users whose devices do not support the crop action
     catch (ActivityNotFoundException anfe) {
      // display an error message
      String errorMessage = "Whoops - your device doesn't support the crop action!";
      Toast toast = Toast.makeText(MainActivity.this, errorMessage, Toast.LENGTH_SHORT);
      toast.show();
      }
     }
    
    
    
    
     @Override
     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
     super.onActivityResult(requestCode, resultCode, data);
    
     if (requestCode == PIC_CROP) {
      if (data != null) {
          // get the returned data
          Bundle extras = data.getExtras();
          // get the cropped bitmap
          Bitmap selectedBitmap = extras.getParcelable("data");
          FileOutputStream ostream;
        try {
            ostream = new FileOutputStream(file);
            selectedBitmap.compress(CompressFormat.PNG, 10, ostream);
             ostream.close(); 
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
          iv.setImageBitmap(selectedBitmap);
      }
      }
      }  
    

    snap shots

    My Layout snap shot

    enter image description here

    On button click prompting the user to enter a name to save

    enter image description here

    Image cropped and saved opened in gallery

    enter image description here

    Note: I have saved the cropped image in sdcard. The snap shots size varies. This worked for me on my device. I have posted the same. If anyone has a better idea pls edit the above post.

    Manifestfile

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.cropimagefromsdcard"
    android:versionCode="1"
    android:versionName="1.0" >
    
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.cropimagefromsdcard.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>
    </application>
    
    0 讨论(0)
提交回复
热议问题