【问题标题】:Save name for screenshots and with mobile time and date保存屏幕截图的名称以及移动时间和日期
【发布时间】:2014-12-12 17:41:52
【问题描述】:

我想给不同的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>

我的日志猫:

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);
    }
}

【问题讨论】:

  • 所以它不起作用吗?
  • 必须检查您是否在清单文件中添加了写入和读取外部存储的权限
  • 两种解决方案都不起作用??它不是将带有时间和日期的图像保存到特定文件夹吗?
  • 不保存图像,不保存时间和日期
  • 使用这种日期格式。 "MM-dd-yy hh-mm-ss"。如果它显示任何错误?你也可以使用System.currentTimeInMillis()

标签: android image file bitmap


【解决方案1】:
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);
          }

【讨论】:

  • 为什么在新日期需要一个参数
  • 我应该在日期中给出什么参数
【解决方案2】:

试试看-

 // 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();
  }
}

【讨论】:

  • 它在我的应用程序中工作..检查更新的答案。是否有任何错误?
  • @MalaikaKhan 必须检查你是否在清单文件中添加了读写外部存储的权限
  • @yuvaツ 真的很棒。坚持下去。
  • @yuvaツ 欢迎光临!!
  • @MalaikaKhan 检查更新的答案,但您在发布 logcat 信息时仍然遇到问题
猜你喜欢
  • 2020-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-20
  • 1970-01-01
相关资源
最近更新 更多