【问题标题】:How can I upload photos with timestamp as name?如何上传以时间戳为名称的照片?
【发布时间】:2017-11-01 22:10:27
【问题描述】:

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

这里是sn-p的代码:

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

                }
            });


                }

    });
}

【问题讨论】:

    标签: android timestamp rename photo


    【解决方案1】:

    使用类似 -

    String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new Date());
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多