如何上传带时间戳的照片作为名称?

时间:2017-05-31 23:09:28

标签: android timestamp rename photo

我想创建此代码,我上传图片并在上传图片时使用新的时间戳重命名自己,如YYYYMMDDHHMMSS。 如何创建此代码? 谢谢:))

以下是代码片段:

 public class MainActivity extends AppCompatActivity {

private StorageReference mStorage;
private static final int GALLERY_INTENT = 2;
private ImageButton mCamera;
private ImageButton mGallery;
private ImageView mImageview;
private static final int CAMERA_REQUEST_CODE = 1;
private ImageButton mUpload;




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mStorage = FirebaseStorage.getInstance().getReference();
    mGallery = (ImageButton) findViewById(R.id.UpGallery);
    //mProgressDialog = new ProgressDialog(this);
    //mProgressDialog1 = new ProgressDialog(this);
    mUpload = (ImageButton) findViewById(R.id.UpImage);

    mCamera = (ImageButton) findViewById(R.id.UpCamera);
    mImageview = (ImageView) findViewById(R.id.imageView1);
    mImageview.buildDrawingCache();
    mImageview.setDrawingCacheEnabled(true);
    mImageview.buildDrawingCache();




    final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
    File newdir = new File(dir);
    newdir.mkdirs();


    mGallery.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_PICK);
            intent.setType("image/*");
            startActivityForResult(intent, GALLERY_INTENT);
        }
    });

    mCamera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent1, CAMERA_REQUEST_CODE);
        }
    });

    mUpload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final StorageReference storageRef = FirebaseStorage.getInstance().getReferenceFromUrl("gs://lastposted-14247.appspot.com");

            mImageview.setDrawingCacheEnabled(true);
            mImageview.buildDrawingCache();
            Bitmap bitmap = mImageview .getDrawingCache();

            ByteArrayOutputStream baas = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baas);

            byte[] data = baas.toByteArray();

            UploadTask uploadTask = storageRef.child("Photo").child("unique-id-image").putBytes(data);

            uploadTask.addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    Toast.makeText(MainActivity.this, "Picture upload failed", Toast.LENGTH_LONG).show();
                }
            }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                  //String downloadUrl = taskSnapshot.getDownloadUrl();
                    Toast.makeText(MainActivity.this, "Upload done", Toast.LENGTH_LONG).show();

                }
            });


                }

    });
}

1 个答案:

答案 0 :(得分:0)

使用像 -

这样的东西
String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());