保存屏幕截图的名称以及移动时间和日期

时间:2014-10-17 07:23:05

标签: android image file bitmap

我想将名称命名为具有移动日历名称和时间的不同screenshots 请帮忙..

提前致谢。我正在添加我的清单文件,请看看。请加上进一步的帮助 我做错了什么

 public class MainActivity extends Activity implements Listener {

//  private static final int CAMERA_REQUEST=0;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     Button btn=(Button)findViewById(R.id.button1);
     btn.setOnClickListener(new OnClickListener() {

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


     if (v.getId() == R.id.button1) {

              Bitmap bitmap = takeScreenshot();

            ImageView imageView = (ImageView)findViewById(R.id.imageView);
            imageView.setImageBitmap(bitmap);

              saveBitmap(bitmap);


          }

      }
     });}

      public Bitmap takeScreenshot() {
          View rootView = findViewById(android.R.id.content).getRootView();
          rootView.setDrawingCacheEnabled(true);
          return rootView.getDrawingCache();
      }
      public void saveBitmap(Bitmap bitmap) {

          try {

              ByteArrayOutputStream bytes = new ByteArrayOutputStream();
              bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

              Calendar cal=Calendar.getInstance();
             File f = new File(Environment.getExternalStorageDirectory()
                    // (cal.getTimeInMillis()+"jpg"));
                      + File.separator+ (cal.getTimeInMillis()+"jpg") );


              f.createNewFile();
              FileOutputStream fo = new FileOutputStream(f);
              fo.write(bytes.toByteArray());
              fo.close();
          } catch (FileNotFoundException e) {
              Log.e("GREC", e.getMessage(), e);
          } catch (IOException e) {
              Log.e("GREC", e.getMessage(), e);
          }







      }


public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}




@Override
public void onGpsStatusChanged(int event) {
    // TODO Auto-generated method stub

}}

这是我的清单文件:

       <xml version="1.0" encoding="utf-8"?>
       <manifest xmlns:android="http://schemas.android.com/apk/res/android"
       package="com.example.imagescreen"
       android:versionCode="1"
       android:versionName="1.0" >
       <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
       <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

       <application
        android:allowBackup="true"
        android:icon="@drawable/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>
        </application>

        </manifest>

我的logcat:

10-18 16:29:17.957: I/dalvikvm(91): Jit: resizing JitTable from 4096 to 8192

10-18 16:29:17.997: D/PermissionCache(36): checking android.permission.READ_FRAME_BUFFER         for uid=1000 => granted (20166 us)

10-18 16:29:17.997: W/WindowManager(91): Failure taking screenshot for (230x409) to layer 21005

10-18 16:29:18.027: D/AndroidRuntime(527): Shutting down VM

 10-18 16:29:18.067: D/dalvikvm(527): GC_CONCURRENT freed 98K, 77% free 480K/2048K, paused 1ms+1ms

10-18 16:29:18.077: I/AndroidRuntime(527): NOTE: attach of thread 'Binder Thread #3' failed

10-18 16:29:18.087: D/dalvikvm(538): Not late-enabling CheckJNI (already on)

10-18 16:29:18.128: I/ActivityManager(91): Start proc com.example.imagescreen for activity com.example.imagescreen/.MainActivity: pid=538 uid=10044 gids={1015}

10-18 16:29:18.377: W/NetworkManagementSocketTagger(91): setKernelCountSet(10044, 1) failed with errno -2

10-18 16:29:18.877: I/Process(91): Sending signal. PID: 538 SIG: 3

10-18 16:29:18.877: I/dalvikvm(538): threadid=3: reacting to signal 3

10-18 16:29:19.007: I/dalvikvm(538): Wrote stack traces to '/data/anr/traces.txt'

10-18 16:29:19.377: I/Process(91): Sending signal. PID: 538 SIG: 3

10-18 16:29:19.377: I/dalvikvm(538): threadid=3: reacting to signal 3

10-18 16:29:19.427: I/dalvikvm(538): Wrote stack traces to '/data/anr/traces.txt'

10-18 16:29:19.657: D/gralloc_goldfish(538): Emulator without GPU emulation detected.

10-18 16:29:19.737: I/ActivityManager(91): Displayed com.example.imagescreen/.MainActivity: +1s717ms

10-18 16:29:20.367: W/NetworkManagementSocketTagger(91): setKernelCountSet(10013, 0) failed with errno -2

10-18 16:29:20.387: W/InputManagerService(91): Starting input on non-focused client com.android.internal.view.IInputMethodClient$Stub$Proxy@412e7248 (uid=10013 pid=189)

10-18 16:29:22.233: D/ExchangeService(391): Received deviceId from Email app: androidc259148960

10-18 16:29:22.233: D/ExchangeService(391): Reconciling accounts...

10-18 16:29:36.067: D/dalvikvm(538): GC_FOR_ALLOC freed 113K, 4% free 6680K/6919K, paused 76ms

10-18 16:29:36.097: I/dalvikvm-heap(538): Grow heap (frag case) to 8.153MB for 1639696-byte allocation

10-18 16:29:36.157: D/dalvikvm(538): GC_CONCURRENT freed 3K, 4% free 8278K/8583K, paused 6ms+4ms

10-18 16:30:10.347: W/ThrottleService(91): unable to find stats for iface rmnet0

我的位图: public void saveBitmap(Bitmap bitmap){

    try {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
        File f = new File(Environment.getExternalStorageDirectory()
                + File.separator + "test.jpg");
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
}

2 个答案:

答案 0 :(得分:0)

试一试 -

 // take screenshot
 public Bitmap takeScreenshot() {
      View v1 = getWindow().getDecorView().getRootView();
     v1.setDrawingCacheEnabled(true);
     return Bitmap.createBitmap(v1.getDrawingCache());
  }

保存图片

File fn;
public void onSnapshotReady(Bitmap bitmap) {
   // TODO Auto-generated method stub
    bitmap 
    try {
    File sampleDir = new File(Environment.getExternalStorageDirectory(), "/ScreenShots");
   // Created directory if not exist
   if(!sampleDir.exists()){
   sampleDir.mkdirs();
   }
   Date d=new Date();
  fn=new File(sampleDir+"/"+"Screenshot"+d.getTime()+".png");
  FileOutputStream out = new FileOutputStream(fn);
  bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
  Toast.makeText(getApplicationContext(), "Screenshot saved at - "+sampleDir+"!!!",Toast.LENGTH_SHORT).show();
  } catch (Exception e) {
  e.printStackTrace();
  }
}

答案 1 :(得分:0)

public void saveBitmap(Bitmap bitmap) {

          try {

              ByteArrayOutputStream bytes = new ByteArrayOutputStream();
              bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

              SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
              String currentDateandTime = sdf.format(new Date());
              File f = new File(Environment.getExternalStorageDirectory()
                 + File.separator+ (currentDateandTime+".jpg") );


              f.createNewFile();
              FileOutputStream fo = new FileOutputStream(f);
              fo.write(bytes.toByteArray());
              fo.close();
          } catch (FileNotFoundException e) {
              Log.e("GREC", e.getMessage(), e);
          } catch (IOException e) {
              Log.e("GREC", e.getMessage(), e);
          }